Question: Error or misconcept

Emile van Sebille emile at fenx.com
Mon Nov 5 07:58:54 EST 2001


"Károly Ladvánszky" <aa at bb.cc> wrote in message
news:3be65da4$1_3 at corp-goliath.newsgroups.com...
> I have run into the following problems.
>
> 1.)
>
> class c1:
>     a1=[]


You've just created a class level shared object a1.  Remove this and add the
following if you want instance level:

    def __init__(self):
        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.
<snip>

HTH,


--

Emile van Sebille
emile at fenx.com

---------





More information about the Python-list mailing list