staticmethod behaviour

S

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
 
S

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

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

Peter Otten

Samu said:
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
 
S

Samu

Samu said:
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?
 
P

Peter Otten

Samu said:
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
 
S

Samu

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top