staticmethod behaviour

2010-08-25 Thread Samu
Hi,

I run today into some problems with my code and I realized that there
is something in the behaviours of the @staticmethod that I don't
really understand. I don't know if it is an error or not, actually,
only that it was, definitely, unexpected.

I wrote a small demo of what happens.

The code:
http://dpaste.com/hold/233795/

The answer I get:
User created with static: id, rights, rights2
1 ['read', 'write'] ['write2'] ['write3']
User created with User()
None [] ['write2'] ['write3']

I was expecting either all arrays from the second to be [] or to be a
copy of the first one.

If someone can provide an explanation, I would be thankful :)

Regards,
Samu
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: staticmethod behaviour

2010-08-25 Thread Samu
On Aug 25, 3:03 pm, Samu  wrote:
> Hi,
>
> I run today into some problems with my code and I realized that there
> is something in the behaviours of the @staticmethod that I don't
> really understand. I don't know if it is an error or not, actually,
> only that it was, definitely, unexpected.
>
> I wrote a small demo of what happens.
>
> The code:http://dpaste.com/hold/233795/
>
> The answer I get:
> User created with static: id, rights, rights2
> 1 ['read', 'write'] ['write2'] ['write3']
> User created with User()
> None [] ['write2'] ['write3']
>
> I was expecting either all arrays from the second to be [] or to be a
> copy of the first one.
>
> If someone can provide an explanation, I would be thankful :)
>
> Regards,
> Samu

In addition, if I don't define the function as static, but either as a
method of the object or a function outside of the class, something
like this:
def cr_user():
user = User(1, ['read'], rights3=[])
user.rights.append('write')
user.rights2.append('write2')
user.rights3.append('write3')
return user

I get instead:
User created with static: id, rights, rights2
1 ['read', 'write'] ['write2'] ['write3']
User created with User()
None [] ['write2'] []

There is some (maybe deep) concept that I don't get it seems, because
that output puzzles me...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: staticmethod behaviour

2010-08-25 Thread Samu
On Aug 25, 3:26 pm, Peter Otten <[email protected]> wrote:
> Samu wrote:
> > Hi,
>
> > I run today into some problems with my code and I realized that there
> > is something in the behaviours of the @staticmethod that I don't
> > really understand. I don't know if it is an error or not, actually,
> > only that it was, definitely, unexpected.
>
> > I wrote a small demo of what happens.
> > The code:
> > class User:
> >  def __init__(self, id=None, rights=[], rights2=[], rights3=[]):
> >   self.id = id
> >   self.rights = rights
> >   self.rights2 = rights2
> >   self.rights3 = rights3
> > �...@staticmethod
> >  def cr_user():
> >   user = User(1, ['read'], rights3=[])
> >   user.rights.append('write')
> >   user.rights2.append('write2')
> >   user.rights3.append('write3')
> >   return user
>
> > print "User created with static: id, rights, rights2"
> > a = User.cr_user()
> > print a.id, a.rights, a.rights2, a.rights3
> > print "User created with User()"
> > b = User()
> > print b.id, b.rights, b.rights2, a.rights3
> > The answer I get:
> > User created with static: id, rights, rights2
> > 1 ['read', 'write'] ['write2'] ['write3']
> > User created with User()
> > None [] ['write2'] ['write3']
>
> > I was expecting either all arrays from the second to be [] or to be a
> > copy of the first one.
>
> > If someone can provide an explanation, I would be thankful :)
>
> The problem is not the staticmethod, it's the mutable default values for
> __init__(). See
>
> http://effbot.org/zone/default-values.htm
>
> Peter

Ahh, thank you very much for the link. Now I understand. I remember
having read that before, but it is not until you face the problem that
the concept sticks. But why does it have a different behaviour the
staticmethod with the "rights3" case then?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: staticmethod behaviour

2010-08-25 Thread Samu
On Aug 25, 4:32 pm, Peter Otten <[email protected]> wrote:
> Samu wrote:
> > the concept sticks. But why does it have a different behaviour the
> > staticmethod with the "rights3" case then?
>
> Moving from staticmethod to standalone function doesn't affect the output.
> You have inadvertently changed something else.
>
> Peter

Absolutely right. Thank you very much for your time and answers,
Peter :) It helped me a lot!
-- 
http://mail.python.org/mailman/listinfo/python-list