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. 3 1133
"Cndistin" <cn******@aol.com> wrote in message
news:20***************************@mb-m01.aol.com... 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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: John M. Gabriele |
last post by:
I've done some C++ and Java in the past, and have recently learned
a fair amount of Python. One thing I still really don't get though
is the difference between class methods and instance methods. I...
|
by: MFC |
last post by:
Ok, after three C# books, (C# How to Program, Programming in the Key
of C#, and C# Weekend Crash Course) and three weeks, I believe I have
tried everything to make a certain form function...
|
by: Dan Trowbridge |
last post by:
Hi everyone,
In my attempt to port code from VS 6.0 to VS.NET I had some code break along
the way, mostly due to not adhereing closely to the C++ standard. This may
be another instance but I...
|
by: serge calderara |
last post by:
Dear all,
I am new in asp.net and prepare myself for exam
I still have dificulties to understand the difference between server control
and HTML control.
Okey things whcih are clear are the fact...
|
by: Sam |
last post by:
Hi,
I have a serious issue using multithreading. A sample application
showing my issue can be downloaded here:
http://graphicsxp.free.fr/WindowsApplication11.zip
The problem is that I need to...
|
by: Anup Daware |
last post by:
Hi Group,
I have a little confusion over the use of static class in C#.
I have a static method in my static class. This method reads an xml
and returns a collection of objects.
This collection...
|
by: rookiejavadude |
last post by:
I'm have most of my java script done but can not figure out how to add a few buttons. I need to add a delete and add buttong to my existing java program. Not sure were to add it on how. Can anyone...
|
by: yadin |
last post by:
class big(self):
x = 32
list =
def inside (self):
class small(self): # a new class defined inside the first
y = 348
list.append(y) # send the value to first list
|
by: =?Utf-8?B?U2l2?= |
last post by:
I have a form that I programmatically generate some check boxes and labels on.
Later on when I want to draw the form with different data I want to clear
the previously created items and then put...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |