help needed with class and method confusion

Discussion in 'Python' started by Cndistin, Jan 6, 2004.

  1. Cndistin

    Cndistin Guest

    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.
    Cndistin, Jan 6, 2004
    #1
    1. Advertising

  2. Cndistin

    Sean Ross Guest

    "Cndistin" <> wrote in message
    news:...
    > 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.


    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.append(moon)

    a = Application()
    print a.moons

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

    HTH
    Sean
    Sean Ross, Jan 6, 2004
    #2
    1. Advertising

  3. Cndistin

    EricN Guest

    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(self, name):
    self.NAME = str(name)

    def getMoonName(self):
    return self.NAME

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

    def getMoonDiameter(self):
    return self.DIAMETER

    def setMoonHomePlanet(self, planet):
    self.HOMEPLANET = str(planet)

    def getMoonHomePlanet(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(Europa)
    Ganymeade = Moon("Ganymeade", 3.0, "Jupiter")
    moons.append(Ganymeade)
    Titan = Moon("Titan", 3.0, "Saturn")
    moons.append(Titan)

    for x in range(len(moons)):
    print moons[x].getMoonName()
    print moons[x].getMoonDiameter()
    print moons[x].getMoonHomePlanet()
    print
    EricN, Jan 6, 2004
    #3
  4. 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,names):
    self.moons = []
    for name in names:
    m=Moon(name)
    setattr(self, name, m)
    self.moons.append(m)

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

    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
    Pierre Quentel, Jan 6, 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. E11
    Replies:
    1
    Views:
    4,736
    Thomas Weidenfeller
    Oct 12, 2005
  2. Ian
    Replies:
    2
    Views:
    340
    Ulrich Petri
    Sep 6, 2003
  3. Anup Daware
    Replies:
    2
    Views:
    336
    Anup Daware
    Feb 2, 2007
  4. Pawel_Iks
    Replies:
    3
    Views:
    530
    terminator
    Jul 31, 2007
  5. Replies:
    3
    Views:
    925
    Roedy Green
    Jan 28, 2008
Loading...

Share This Page