classes: need for an explanation

0

0k-

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? :)
 
P

Peter Otten

0k- said:
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? :)

Putting an assignment into the class body makes the name a /class/ attribute
which is shared by all instances of the class:
.... props = {} # this is a class attribute
....True

The solution is to move the attribute creation into the initializer:
.... def __init__(self):
.... self.props = {} # an instance attribute
....False

Peter
 
S

Steven D'Aprano

hello!

i started to write a game in python in which i want to implement
"dynamically attachable" attributes.

All attributes in Python are dynamically attachable.
.... pass
....'beautiful red'


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

t1 = Thing()
t2 = Thing()

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


Have you considered a simpler class like this?

class Thing(object):
def __init__(self, value="something important"):
self.text = TxtAttr(value)

t2 = Thing("another string")

Then, instead of writing t2.props["text"].value (four lookups) you would
just write t2.text (two lookups).
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top