python – 如何在参数具有相同名称时还原内置函数?

我知道你是“
not supposed to”使用内置名称作为函数的参数,但有时它们最有意义:

def foo(range=(4,5), type="round", len=2):

但是如果已经完成,并且已经处理了范围变量并且不再需要它,那么如何返回内置范围并在foo()中使用它?

del range不恢复内置:

UnboundLocalError: local variable 'range' referenced before assignment

最佳答案 对于
Python 2.x

import __builtin__
range = __builtin__.range

对于Python 3.x

import builtins
range = builtins.range
点赞