Class attributes newbie question (before I join a cargocult)

E

EP

Hi,

This is likely a stupid question, but I am confused about what is going
on with class attributes as far as whether they "stick". I ran across
this in a program I am writing, but have reproduced the behavoir below
- can someone point me in the right direction (thanks):

class AttStickiness(object):
def __init__(self, myname="", mysex=""):
self.name=myname
self.sex=mysex

def changeSex(self, newsex=""):
self.mysex=newsex
return self.mysex

def changeName(self, newname=""):
self.name=newname
return self.name

def whoAmI(self):
return self.name, self. sex
('Eric', 'Male')
('Jimbo', 'Male')
('Jimbo', 'Male')


[while it is comforting to know my sex isn't so easily changed, this
has confounded me in real code]

thx,

Eric
 
C

Christoph Haas

Am Sonntag, 12. März 2006 19:36 schrieb EP:
This is likely a stupid question, but I am confused about what is going
on with class attributes as far as whether they "stick". I ran across
this in a program I am writing, but have reproduced the behavoir below
- can someone point me in the right direction (thanks):

class AttStickiness(object):
def __init__(self, myname="", mysex=""):
self.name=myname
self.sex=mysex

^--- sex
def changeSex(self, newsex=""):
self.mysex=newsex

^--- mysex

You are not altering the same attribute.

Cheers
Christoph
 
K

kalahari

def changeSex(self, newsex=""):
self.mysex=newsex --->>>>> self.mysex
return self.mysex

def whoAmI(self):
return self.name, self. sex ->>>>>> self.sex
 
P

pclinch

EP said:
Hi,

This is likely a stupid question, but I am confused about what is going
on with class attributes as far as whether they "stick". I ran across
this in a program I am writing, but have reproduced the behavoir below
- can someone point me in the right direction (thanks):

class AttStickiness(object):
def __init__(self, myname="", mysex=""):
self.name=myname
self.sex=mysex

def changeSex(self, newsex=""):
self.mysex=newsex
return self.mysex

You might want self.sex = newsex and return self.sex here.

See __slots__ and PyChecker for possible ways to avoid misspelled
instance variables.
def changeName(self, newname=""):
self.name=newname
return self.name

def whoAmI(self):
return self.name, self. sex
('Eric', 'Male')
('Jimbo', 'Male')
('Jimbo', 'Male')


[while it is comforting to know my sex isn't so easily changed, this
has confounded me in real code]

thx,

Eric

Regards, Paul
 
M

mattjfleming

Should't the changeSex method be:

def changeSex(self, newsex=""):
self.sex = newsex
return self.sex


You're creating a new instance variable at the moment i.e 'self.mysex'
doesn't exist until you call changeSex.
 
E

EP

Thanks. Argh. I've failed to find the behavoir I need to understand.
More coffee and re-reading my code. Thanks!
 
S

Steven D'Aprano

See __slots__ and PyChecker for possible ways to avoid misspelled
instance variables.

__slots__ are not meant as a way to implement variable declarations.
__slots__ are meant as a memory optimization for cases where you have
large numbers (tens of thousands?) of almost identical instance variables
and you don't need to dynamically add or delete attributes.

Of course, just because __slots__ were invented for one purpose doesn't
mean that you can't find another use for them, but this is not the way.
PyChecker is a good solution for testing for misspelled variables. Another
way is to avoid setters and getters: instead of creating a method to
change the name and sex of the instance, just change the attributes
directly. Not getters and setters means less code, and less code means
fewer bugs (whether due to misspellings or not).


This is not a very Pythonic idiom. (I'm not saying that it is *wrong*,
merely that it is unusual.) Generally, methods should implement one of two
behaviours, as illustrated by the following simplified example:

class Number(object):
def __init__(self, num):
self.value = num

def __add__(self, other):
"""add two instances of Number and return a new instance"""
return Number(self.value + other.value)

def increment(self, inc):
"""increment this instance in place"""
self.value += inc
10

In other words, methods should generally return either a new instance, or
nothing. Unless you have a good reason for it to act differently.
 
P

Peter Otten

EP said:
This is likely a stupid question,

There seems to be a cult believing that calling one's own question "stupid"
magically diminishes its degree of stupidity. In reality, as a compiler
would put it, "code has no effect".
but I am confused about what is going
on with class attributes as far as whether they "stick". I ran across
this in a program I am writing, but have reproduced the behavoir below
- can someone point me in the right direction (thanks):

class AttStickiness(object):
def __init__(self, myname="", mysex=""):
self.name=myname
self.sex=mysex

def changeSex(self, newsex=""):
self.mysex=newsex
return self.mysex

def changeName(self, newname=""):
self.name=newname
return self.name

def whoAmI(self):
return self.name, self. sex

Are empty strings reasonable defaults for name or sex? No.
Do the changeXXX() methods /conceptionally/ anything except change an
attribute? No.
Is whoAmI() meant to inform the user/developer? Yes.

If your answers were like mine you are already deeply into "cargocult".
Instead of changeXXX() use attributes directly (you can turn them into
properties should side effects become necessary), instead of whoAmI() use
__str__() or __repr__().
.... def __init__(self, name, sex):
.... self.name = name
.... self.sex = sex
.... def __repr__(self):
.... return "Person(name=%r, sex=%r)" % (self.name, self.sex)
....Person(name='Jimbo', sex='female')

Peter
 

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

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top