2024-04-20 00:11:50 发布
网友
我知道您正在“not supposed to”使用内置名称作为函数的参数,但有时它们最有意义:
def foo(range=(4,5), type="round", len=2):
但是如果这样做了,并且range变量已经被处理并且不再需要了,那么如何返回到内置的range并在foo()中使用它呢?你知道吗
range
foo()
^{}不还原内置项:
UnboundLocalError: local variable 'range' referenced before assignment
同样对于这两个python版本,您可以使用__builtins__,而无需导入任何内容。你知道吗
__builtins__
示例-
>>> def foo(range=(4,5)): ... print(range) ... range = __builtins__.range ... print(range) ... >>> foo() (4, 5) <class 'range'>
对于Python 2.x
import __builtin__ range = __builtin__.range
对于Python 3.x
import builtins range = builtins.range
同样对于这两个python版本,您可以使用
__builtins__
,而无需导入任何内容。你知道吗示例-
对于Python 2.x
对于Python 3.x
相关问题 更多 >
编程相关推荐