#def大致相当于: _defaultArgs =([],) method = new.function(_realmethod.func_code,globals() ,''方法'', _defaultArgs) 每次重新执行def时,都会得到一组新的默认参数 进行了评估,但评估的结果只是传递给函数构造函数的价值。 代码对象先编译然后编译从代码对象,全局字典,函数名称和 /> 默认参数(以及任何闭包,但它有点难以 说明这种方式)。<snipped erroneous comparison>No, it is closer to:# Assume you have done this earlier:import newdef _realmethod(n, bits):bits.append(n)print bits# The def is roughly equivalent to this:_defaultArgs = ([], )method = new.function(_realmethod.func_code, globals(), ''method'',_defaultArgs)Each time you re-execute the def you get a new set of default argumentsevaluated, but the result of the evaluation is simply a value passed in tothe function constructor.The code object is compiled earlier then def creates a new function objectfrom the code object, the global dictionary, the function name, and thedefault arguments (and any closure as well, but its a bit harder toillustrate that this way). 这篇关于关键字参数 - 奇怪的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 10:41
查看更多