classes: need for an explanation

0k- kuldjlevelet at gmail.com
Thu Jan 18 12:58:22 CET 2007


hello!

i started to write a game in python in which i want to implement
"dynamically attachable" attributes.
my intention is to generalise this and moreover these attributes can be
complex, so i'm creating classes to represent these attributes. but
somehow i cannot instantiate these attribute classes well...

here is the simple outline of my code:

class Attr(object):
    name = ""
    value = ""
    def __init__(self, name, value):
        self.name = name
        self.value = value

# this is just an example, in the real game attrs. are more complex!
class TxtAttr(Attr):
    name = "text"
    def __init__(self, value):
        self.value = value

class Thing(object):
    props = {}
    def __init__(self):
        self.props["text"] = TxtAttr("something important")

t1 = Thing()
t2 = Thing()

t2.props["text"].value = "another string"

print "t1: %s\nt2: %s" % (t1.props["text"].value,
t2.props["text"].value)

the above code outputs:

t1: another string
t2: another string

so the problem i cannot get through is that both t1 and t2 have the
same attr class instance.
could somebody please explain me why? :)




More information about the Python-list mailing list