staticmethod behaviour

Samu samufuentes at gmail.com
Wed Aug 25 15:18:37 CEST 2010


On Aug 25, 3:03 pm, Samu <samufuen... at gmail.com> 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...



More information about the Python-list mailing list