English 中文(简体)
落空的灰色功能参数
原标题:Where is the default parameter in Python function
  • 时间:2012-04-12 09:32:24
  •  标签:
  • python

我认为,许多人看到了得到缺省参数的假日功能。 例如:

def foo(a=[]):
    a.append(3)
    return a

如果我们使用“oo”来形容这一功能,那么在发出呼吁之后,产出将每增加3类。

在界定这一职能时,在目前环境中界定了称为“oo”的功能目标,同时对违约参数值进行评估。 每当该功能被称作无参数时,根据该代码,被评价的参数价值将发生变化。

My question is, where is this evaluated parameter exist? Is it in the function object or it is in the method object when calling the function? Since everything in python is a object, there must be some place to hold the name->value binding of a -->evaluated parameter. Am I over-thinking this problem?

最佳回答

正如其他人已经说过的那样,违约数值储存在功能标的中。

例如,在CPython,你可以这样做:

>>> def f(a=[]):
...     pass
...
>>> f.func_defaults
([],)
>>> f.func_code.co_varnames
( a ,)
>>>

然而,co_varnames可能含有不止一个增产物的名称,因而需要进一步处理,这些属性甚至可能不会存在于其他 Python执行中。 因此,你应使用<代码>内注明模块,而该模块则照顾到你们的所有执行细节:

>>> import inspect
>>> spec = inspect.getargspec(f)
>>> spec
ArgSpec(args=[ a ], varargs=None, keywords=None, defaults=([],))
>>>

<代码>ArgSpec是个名称的图表,使你能够获取所有作为属性的价值:

>>> spec.args
[ a ]
>>> spec.defaults
([],)
>>>

http://docs.python.org/library/inspect.html 说 明:<代码>faults 图表总是与<代码>args的最后论点相对应。 这使你了解情况。

create:

>>> dict(zip(spec.args[-len(spec.defaults):], spec.defaults))
{ a : []}
>>>
问题回答

该表附在功能标上,见foo.func_defaults:

>>> foo()
>>> foo.func_defaults
([3],)
>>> foo()
>>> foo.func_defaults
([3, 3],)

如欲在<代码>上绘制<>a>>的地图,请查阅<代码>foo.func_code:

defaults = foo.func_defaults
# the args are locals in the beginning:
args = foo.func_code.co_varnames[:foo.func_code.co_argcount] 
def_args = args[-len(defaults):]  # the args with defaults are in the end
print dict(zip(def_args, defaults)) # { a : []}

(粗略地说,黄 version版较好。)

在职能标的中,见func_defaults:

def f(a=[]): a.append(3)

print f.func_defaults # ([],)

f()

print f.func_defaults # ([3],)

该功能物体的特性载于<代码>func_defaults。

>>> foo.func_defaults
([],)
>>> foo()
([3],)

我发现了一个有趣的情况:在5.2版的python,尝试功能 f。

>>> foo()
[1]
>>> foo()
[1]
>>> foo()
[1]

由于所要求职能的目的不同:

>>> id(foo())
4336826757314657360
>>> id(foo())
4336826757314657008
>>> id(foo())
4336826757314683160

在2.7.2版本中:

>>> foo()
[1]
>>> foo()
[1, 1]
>>> foo()
[1, 1, 1]

In this case, the object is the same each time calling the function:

>>> id(foo())
29250192
>>> id(foo())
29250192
>>> id(foo())
29250192

它是否是一个不同版本的问题?

www.un.org/Depts/DGACM/index_spanish.htm

>>> def foo(a=[]):
...     a.append(3)
...     return a
... 
>>> foo.__defaults__
([],)




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签