Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > simple class question

Reply
Thread Tools

simple class question

 
 
C GIllespie
Guest
Posts: n/a
 
      01-20-2004
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


 
Reply With Quote
 
 
 
 
Matt Grayson
Guest
Posts: n/a
 
      01-20-2004
On Jan 20, 2004, at 12:34 PM, 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?


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

>
> Many thanks
>
> Colin
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list


------------------------------------------------
Matt Grayson

School of Information Sciences, University of Tennessee
-------------------------------------------------


 
Reply With Quote
 
 
 
 
anton muhin
Guest
Posts: n/a
 
      01-20-2004
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.
 
Reply With Quote
 
Michael Spencer
Guest
Posts: n/a
 
      01-20-2004
"anton muhin" <(E-Mail Removed)> wrote in message
news:bujsd7$iot2m$(E-Mail Removed)-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]
>>>




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple Newbie question about accessing a Variable out of a class of a class Christian Maier C++ 3 02-15-2007 08:24 AM
Class A contains class B, class B points to class A Joseph Turian C++ 5 12-30-2005 03:24 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
A parameterized class (i.e. template class / class template) is not a class? christopher diggins C++ 16 05-04-2005 12:26 AM
Really simple: how to call a class from another class Loic Java 2 11-26-2004 12:37 PM



Advertisments