473,394 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

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

Sep 27 '06 #1
5 1100
da***********@gmail.com 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
Sep 27 '06 #2
da***********@gmail.com 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>

Sep 27 '06 #3
da***********@gmail.com 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
Sep 27 '06 #4
On Wed, 2006-09-27 at 09:32 -0700, da***********@gmail.com 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?

Sep 27 '06 #5
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>

Sep 27 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

90
by: Mark Hahn | last post by:
"Michael Geary" <Mike@Geary.com> wrote ... >Does anyone have some sample code where obj$func() would be used? > (Apologies if I missed it.) There have been so many messages about delegation...
12
by: Egil M?ller | last post by:
Is there any way to create transparent wrapper objects in Python? I thought implementing __getattribute__ on either the wrapper class or its metaclass would do the trick, but it does not work for...
4
by: Brano Zarnovican | last post by:
Hi ! If I define 'f' like this def f(a): print a then, the call with keywords f(1, optional=2)
7
by: JT | last post by:
how can i declare a function that will accept an optional parameter? something like: function newFunc(strValue1, strValue2) --where strValue2 is optional. thanks much.
16
by: G Patel | last post by:
Hi, If I want to call functions that don't return int without declaring them, will there be any harm? I only want to assign the function(return value) to the type that it returns, so I don't...
3
by: Crirus | last post by:
I need to set a parameter for a function as optional and color... how can I do that, because seems I cant set structure as optional parameter -- Cheers, Crirus ------------------------------...
37
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as...
24
by: dmitrey | last post by:
Hi all, I looked to the PEPs & didn't find a proposition to remove brackets & commas for to make Python func call syntax caml- or tcl- like: instead of result = myfun(param1, myfun2(param5,...
1
by: J. Frank Parnell | last post by:
arrrrrg: Condo for rent has 3 price tiers (for different times of the year): value regular premium For every 7 nites they stay, they get 1 free, and that free one should be the cheapest night...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.