simple class question

C

C GIllespie

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
 
M

Matt Grayson

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?

Instead of declaring Y as a new instance of species, just set Y equal
to X:

x = species(pop=10)
y = x
x.pop = 15
print y.pop
 
A

anton muhin

C said:
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.
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top