Question: Error or misconcept
Károly Ladvánszky
aa at bb.cc
Mon Nov 5 04:51:19 EST 2001
I have run into the following problems.
1.)
class c1:
a1=[]
def f1(self,a):
self.a1.append(a)
def test1():
o1=c1()
o1.f1(99)
def test2():
o1=c1()
print o1.a1
Running test1(), test2(), in test2 I'd expect o1.a1 be []. This is not the
case, test2() prints [99] !
My guess is self.a1.append(a) modifies the class object's a1 member, not the
instance object's a1.
When a1 is a number, everything goes as expected:
class c1:
a1=0
def f1(self,a):
self.a1=(a)
def test1():
o1=c1()
o1.f1(99)
def test2():
o1=c1()
print o1.a1
Now test2() prints 0.
2.)
I thought of writing a tiny funtion that'd present a report on an object
given as parameter.
Processing the object's __dict__ special attribute seemed to be a good
choice.
It works fine for the class:
class c1:
a1=0
def f1(self,a):
self.a1=(a)
c1.__dict__ ==> {'__doc__': None, '__module__': '__main__', 'f1': <function
f1 at 012E0BFC>, 'a1': 0}
Trying to use it for an object results in an empty dictionary:
o1=c1()
o1.__dict__ ==> {}
Referring to an attribute of o1 through the dictionary seems to sortof
refreshing the dictionary:
o1.__dict__['a1']=2
o1.__dict__ ==> {'a1': 2}
Is it a misconcept of mine about __dict__? If it is, what's the right way to
enlist an arbitrary object's attributes, methods?
I'm using ActivePython build 2.1.212
Thanks for any help.
Cheers,
Károly
______________________________________________________________________________
Posted Via Binaries.net = SPEED+RETENTION+COMPLETION = http://www.binaries.net
More information about the Python-list
mailing list