On 2007-05-23, Wildemar Wildenburger <[EMAIL PROTECTED]> wrote:
> Antoon Pardon wrote:
>>> This is a FAQ. Default arguments are only evaled once - when the def
>>> statement is evaled (which is usually at import time). The solution is
>>> simple: don't use mutable objects as default arguments:
>>
Alan Franzoni a écrit :
> Il 23 May 2007 04:53:55 -0700, Siah ha scritto:
>
> [cut]
>
> No.
>
> It's because the *body* of the function gets evaluated every time the
> function is called, while the *definition* of the function gets evaluated
> just once, when the function is 'declared'.
>
> You
Il 23 May 2007 04:53:55 -0700, Siah ha scritto:
[cut]
No.
It's because the *body* of the function gets evaluated every time the
function is called, while the *definition* of the function gets evaluated
just once, when the function is 'declared'.
Your issue arises when the default value of the f
Bruno,
I got my lesson today, first get your morning coffee before posting
and of course avoid mutable objects as default arguments. Silly post,
perhaps. Good to hear universe is in place. I should have rtffaq,
though that's not why she left...
Sia
--
http://mail.python.org/mailman/listinfo/pyt
Antoon Pardon wrote:
>> This is a FAQ. Default arguments are only evaled once - when the def
>> statement is evaled (which is usually at import time). The solution is
>> simple: don't use mutable objects as default arguments:
>>
>
> An immutable object would have given the same behaviour in
Siah wrote:
> I think that's because:
No idea what is because of what. Please quote essential parts of the posting
you refer to.
[] is []
> False
() is ()
> True
This is an implementation artifact. The interpreter chose to create only one
instance for the empty tuple for optimization
I think that's because:
>>> [] is []
False
>>> () is ()
True
--
Sia
--
http://mail.python.org/mailman/listinfo/python-list
On 2007-05-23, Bruno Desthuilliers <[EMAIL PROTECTED]> wrote:
> Siah a écrit :
>> Ready to go insane here. Class A, taking on a default value for a
>> variable. Instantiating two separate objects of A() gives me a shared
>> val list object. Just see the example bellow:
>>
>>
>> class A(object):
>
Thanks Alan,
I am still perplexed why the default value of this object is shared.
hemm...d
Back to programming, :)
Sia
On May 23, 7:19 am, Alan Franzoni
<[EMAIL PROTECTED]> wrote:
> Il 23 May 2007 04:07:19 -0700, Siah ha scritto:
>
> > Ready to go insane here. Class A, taking on a default value
Siah a écrit :
> Ready to go insane here. Class A, taking on a default value for a
> variable. Instantiating two separate objects of A() gives me a shared
> val list object. Just see the example bellow:
>
>
> class A(object):
> def __init__(self, val=[]):
> self.val=val
>
> obj1 = A(
Il 23 May 2007 04:07:19 -0700, Siah ha scritto:
> Ready to go insane here. Class A, taking on a default value for a
__init__ is a function, taking a default value of [], which is a list,
which is a mutable object. That said, you should remember that this means
that such default value is 'shared'
Ready to go insane here. Class A, taking on a default value for a
variable. Instantiating two separate objects of A() gives me a shared
val list object. Just see the example bellow:
class A(object):
def __init__(self, val=[]):
self.val=val
obj1 = A()
obj2 = A()
print obj1 is obj2
12 matches
Mail list logo