simple class question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • C GIllespie

    simple class question

    Dear all,

    I'm new to both python and OOP, so could I ask a simple question.

    I have class:

    class species:
    __init__(self,p op=0):
    self.pop=pop

    Now I want to do something like this:

    X=species(pop=1 0)
    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


  • Matt Grayson

    #2
    Re: simple class question

    On Jan 20, 2004, at 12:34 PM, C GIllespie wrote:
    [color=blue]
    > Dear all,
    >
    > I'm new to both python and OOP, so could I ask a simple question.
    >
    > I have class:
    >
    > class species:
    > __init__(self,p op=0):
    > self.pop=pop
    > Now I want to do something like this:
    >
    > X=species(pop=1 0)
    > 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?[/color]

    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
    [color=blue]
    >
    > Many thanks
    >
    > Colin
    >
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list[/color]

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

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


    Comment

    • anton muhin

      #3
      Re: simple class question

      C GIllespie wrote:[color=blue]
      > Dear all,
      >
      > I'm new to both python and OOP, so could I ask a simple question.
      >
      > I have class:
      >
      > class species:
      > __init__(self,p op=0):
      > self.pop=pop
      >
      > Now I want to do something like this:
      >
      > X=species(pop=1 0)
      > 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
      >
      >[/color]

      In some cases pop should be class attribute.

      regards,
      anton.

      Comment

      • Michael Spencer

        #4
        Re: simple class question

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



        Comment

        Working...