help needed with class and method confusion

C

Cndistin

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.
 
S

Sean Ross

Cndistin said:
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
 
E

EricN

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
 
P

Pierre Quentel

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top