Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Why don't optional mutable objects show up in vars(func)?

Reply
Thread Tools

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

 
 
dannycolligan@gmail.com
Guest
Posts: n/a
 
      09-27-2006
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:

>>> def func(x, y=[]):

.... 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?

>>> vars(func)

{}
>>> dir(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

 
Reply With Quote
 
 
 
 
Georg Brandl
Guest
Posts: n/a
 
      09-27-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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:
>
>>>> def func(x, y=[]):

> ... 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?
>
>>>> vars(func)

> {}
>>>> dir(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:

>>> def f(x=1):

.... pass
....
>>> print f.func_defaults

(1,)
>>>


Georg
 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      09-27-2006
(E-Mail Removed) wrote:

> 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:
>
>>>> def func(x, y=[]):

> ... 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?


>>> func.func_defaults

([],)

</F>

 
Reply With Quote
 
=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
Guest
Posts: n/a
 
      09-27-2006
(E-Mail Removed) schrieb:
> 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:
>
>>>> def func(x, y=[]):

> ... 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.

>>>> vars(func)

> {}
>>>> dir(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.


Take a look at func_defaults.

Regards,
Martin
 
Reply With Quote
 
Sean Hammond
Guest
Posts: n/a
 
      09-27-2006
On Wed, 2006-09-27 at 09:32 -0700, (E-Mail Removed) wrote:
> 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:
>
> >>> 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?

 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      09-27-2006
Sean Hammond wrote:

>>>>> 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>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Mutable Objects and Thread Boundaries Alan Gutierrez Java 43 07-24-2010 04:39 AM
Are routine objects guaranteed mutable & with dictionary? Alf P. Steinbach Python 15 12-07-2009 07:36 AM
Prototypal inheritance and mutable objects Nick Fletcher Javascript 3 03-30-2008 05:35 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Re: (objects as) mutable dictionary keys Peter Maas Python 8 01-17-2005 12:52 PM



Advertisments