Why don't optional mutable objects show up in vars(func)?

D

dannycolligan

So I just got bitten by the "don't use a mutable object as an optional
argument" gotcha. I now realize that for this function:
.... y.append(x)
.... print y
....

y is initialized when the function is imported, not when the function
is executed. However, if this is the case, then why is y not showing
up as an attribute of func?
['__call__', '__class__', '__delattr__', '__dict__', '__doc__',
'__get__', '__getattribute__', '__hash__', '__init__', '__module__',
'__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
'__setattr__', '__str__', 'func_closure', 'func_code', 'func_defaults',
'func_dict', 'func_doc', 'func_globals', 'func_name']

I'm using Python 2.4.3, if that is at all relevant. Thanks in advance
for any help.

Danny
 
G

Georg Brandl

So I just got bitten by the "don't use a mutable object as an optional
argument" gotcha. I now realize that for this function:
... y.append(x)
... print y
...

y is initialized when the function is imported, not when the function
is executed. However, if this is the case, then why is y not showing
up as an attribute of func?
['__call__', '__class__', '__delattr__', '__dict__', '__doc__',
'__get__', '__getattribute__', '__hash__', '__init__', '__module__',
'__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
'__setattr__', '__str__', 'func_closure', 'func_code', 'func_defaults',
'func_dict', 'func_doc', 'func_globals', 'func_name']

I'm using Python 2.4.3, if that is at all relevant. Thanks in advance
for any help.

y is not an attribute of func, it's a default parameter value and as such
stored in func_defaults:
.... pass
....
Georg
 
F

Fredrik Lundh

So I just got bitten by the "don't use a mutable object as an optional
argument" gotcha. I now realize that for this function:
... y.append(x)
... print y
...

y is initialized when the function is imported, not when the function
is executed. However, if this is the case, then why is y not showing
up as an attribute of func?
([],)

</F>
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

So I just got bitten by the "don't use a mutable object as an optional
argument" gotcha. I now realize that for this function:
... y.append(x)
... print y
...

y is initialized when the function is imported, not when the function
is executed. However, if this is the case, then why is y not showing
up as an attribute of func?

Because it's not an attribute of the function object; it's a default
value of the function.
['__call__', '__class__', '__delattr__', '__dict__', '__doc__',
'__get__', '__getattribute__', '__hash__', '__init__', '__module__',
'__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
'__setattr__', '__str__', 'func_closure', 'func_code', 'func_defaults',
'func_dict', 'func_doc', 'func_globals', 'func_name']

I'm using Python 2.4.3, if that is at all relevant. Thanks in advance
for any help.

Take a look at func_defaults.

Regards,
Martin
 
S

Sean Hammond

So I just got bitten by the "don't use a mutable object as an optional
argument" gotcha. I now realize that for this function:
... y.append(x)
... print y
...

y is initialized when the function is imported, not when the function
is executed.

I thought it was initialised the first time the function gets called?
 
F

Fredrik Lundh

Sean said:
def func(x, y=[]):
... y.append(x)
... print y
...

y is initialized when the function is imported, not when the function
is executed.

I thought it was initialised the first time the function gets called?

it's initialized (in the surrounding context) when the "def" statement
is executed.

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top