help needed with class and method confusion

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

    help needed with class and method confusion

    First I am sorry for the title but I an newbie enough to now know how to better
    word it.

    The problem part of my code is
    class Application:
    class Moon:
    def __init__(self, name):
    self.name = name
    def __init__(self):
    self.moons = []
    names = ["Io", "Europa", "Ganymeade"]
    for name in names:
    setattr(self, name, Moon(name))
    I would like to somehow get self.moons to equal
    [self.Io, self.Europa, self.Ganymeade]. I had hoped on using
    self.moons as an iterant in "for" loops to be able to alter each
    in turn.

    Thanks in advance for any possible help.
  • Sean Ross

    #2
    Re: help needed with class and method confusion

    "Cndistin" <cndistin@aol.c om> wrote in message
    news:2004010610 2232.19889.0000 2028@mb-m01.aol.com...[color=blue]
    > The problem part of my code is
    > class Application:
    > class Moon:
    > def __init__(self, name):
    > self.name = name
    > def __init__(self):
    > self.moons = []
    > names = ["Io", "Europa", "Ganymeade"]
    > for name in names:
    > setattr(self, name, Moon(name))
    > I would like to somehow get self.moons to equal
    > [self.Io, self.Europa, self.Ganymeade]. I had hoped on using
    > self.moons as an iterant in "for" loops to be able to alter each
    > in turn.
    >
    > Thanks in advance for any possible help.[/color]

    class Application:
    class Moon:
    def __init__(self, name):
    self.name = name
    def __repr__(self):
    "added this to pretty up the printing of a.moons"
    return "Moon(%s)"%self .name

    def __init__(self):
    self.moons = []
    names = ["Io", "Europa", "Ganymeade"]
    for name in names:
    # took Moon(name) out of the setattr() because we'll be
    # using it again in moons.append. Also used self.Moon
    # because Moon alone raises a NameError
    moon = self.Moon(name)
    setattr(self, name, moon)
    self.moons.appe nd(moon)

    a = Application()
    print a.moons

    # output
    [Moon(Io), Moon(Europa), Moon(Ganymeade)]

    HTH
    Sean


    Comment

    • EricN

      #3
      Re: help needed with class and method confusion

      Here's one way to do it (undoubtedly there are other ways as well).
      Sorry, I couldn't loop the moons. Probably a programmer more clever
      than I could write a factory pattern for it.

      class Moon:
      def __init__(self, name, diameter = 0.0, planet = "unknown"):
      self.NAME = name
      self.DIAMETER = diameter
      self.HOMEPLANET = planet

      def setMoonName(sel f, name):
      self.NAME = str(name)

      def getMoonName(sel f):
      return self.NAME

      def setMoonDiameter (self, diam):
      self.DIAMETER = float(diam)

      def getMoonDiameter (self):
      return self.DIAMETER

      def setMoonHomePlan et(self, planet):
      self.HOMEPLANET = str(planet)

      def getMoonHomePlan et(self):
      return self.HOMEPLANET


      if __name__ == "__main__":
      moons = []
      Io = Moon("Io", 1.0, "Jupiter")
      moons.append(Io )
      Europa = Moon("Europa", 2.0, "Jupiter")
      moons.append(Eu ropa)
      Ganymeade = Moon("Ganymeade ", 3.0, "Jupiter")
      moons.append(Ga nymeade)
      Titan = Moon("Titan", 3.0, "Saturn")
      moons.append(Ti tan)

      for x in range(len(moons )):
      print moons[x].getMoonName()
      print moons[x].getMoonDiamete r()
      print moons[x].getMoonHomePla net()
      print

      Comment

      • Pierre Quentel

        #4
        Re: help needed with class and method confusion

        If I understand the problem, you have a planet and a number of moons turning
        around it. So you should define two different classes, Planet and Moon

        --------------------------------
        class Moon:

        def __init__(self, name):
        self.name = name

        class Planet:

        def __init__(self,n ames):
        self.moons = []
        for name in names:
        m=Moon(name)
        setattr(self, name, m)
        self.moons.appe nd(m)

        satellites = ["Io", "Europa", "Ganymeade"]
        Jupiter = Planet(satellit es)
        -------------------------------

        You'd better leave the satellite names outside of the __init__ method of
        Planet,
        in case you happen to work on another planet

        Hope this helps,
        Pierre


        Comment

        Working...