Class attributes newbie question (before I join a cargocult)

EP eric.pederson at gmail.com
Sun Mar 12 13:36:15 EST 2006


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

>>> me=AttStickiness("Eric","Male")

>>> me.whoAmI()
('Eric', 'Male')

>>> me.changeName("Jimbo")
'Jimbo'

>>> me.whoAmI()
('Jimbo', 'Male')

>>> me.changeSex("female")
'female'

>>> me.whoAmI()
('Jimbo', 'Male')


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

thx,

Eric




More information about the Python-list mailing list