simple class question

Michael Spencer michael at telcopartners.com
Tue Jan 20 19:49:35 CET 2004


"anton muhin" <antonmuhin at rambler.ru> wrote in message
news:bujsd7$iot2m$1 at ID-217427.news.uni-berlin.de...
> C GIllespie wrote:
> > Dear all,
> >
> > I'm new to both python and OOP, so could I ask a simple question.
> >
> > I have class:
> >
> > class species:
> >     __init__(self,pop=0):
> >         self.pop=pop
> >
> > Now I want to do something like this:
> >
> > X=species(pop=10)
> > Y=species(pop=X.pop)
> > OK, but now I want to update X.pop and have that mirrored in Y.pop, i.e.
if
> > X.pop=5, Y.pop now equals 5.
> >
> > What is the best/nicest/simplest way of doing this?
> >
> > Many thanks
> >
> > Colin
> >
> >
>
> In some cases pop should be class attribute.
>
> regards,
> anton.
Example using class attribute:

>>> class species(object):
...  pop = [0] #Must be mutable object
...  def __init__(self, pop = 0):
...   self.pop[0] = pop
...
>>> x = species(10)
>>> x.pop
[10]
>>> y = species(5)
>>> y.pop
[5]
>>> x.pop
[5]
>>>






More information about the Python-list mailing list