simple class question

Discussion in 'Python' started by C GIllespie, Jan 20, 2004.

  1. C GIllespie

    C GIllespie Guest

    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
     
    C GIllespie, Jan 20, 2004
    #1
    1. Advertising

  2. C GIllespie

    Matt Grayson Guest

    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
    -------------------------------------------------
     
    Matt Grayson, Jan 20, 2004
    #2
    1. Advertising

  3. C GIllespie

    anton muhin Guest

    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.
     
    anton muhin, Jan 20, 2004
    #3
  4. "anton muhin" <> wrote in message
    news:bujsd7$iot2m$-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]
    >>>
     
    Michael Spencer, Jan 20, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    575
  2. E11
    Replies:
    1
    Views:
    4,844
    Thomas Weidenfeller
    Oct 12, 2005
  3. christopher diggins
    Replies:
    16
    Views:
    769
    Pete Becker
    May 4, 2005
  4. Joseph Turian
    Replies:
    5
    Views:
    609
  5. Christian Maier
    Replies:
    3
    Views:
    505
    John Harrison
    Feb 15, 2007
Loading...

Share This Page