[Tutor] Class Nesting

2012-02-06 Thread Greg Nielsen
Hello List,

 My name is Greg, and while working on a project I've come across a
rather interesting problem. I'm trying to create a rough model of a star
cluster and all of the stars and planets contained within. Kind of a cool
project; hopefully it should work a little like this. I create a Star
Cluster object, which goes through a list of positions and decides if it
should build a Star System there. If it does, it then creates a Star System
object at that position which in turn calls upon and creates several Planet
objects to reside inside of it. All in all, about 64 positions to check, on
average 24 Star Systems, each with between 2 and 9 planets.
 So here is the problem, to create an object, you need to assign it to
a variable, and you need to know what that variable is to call upon it
later, so to have a object build a second object, it would need to somehow
create a variable name, and you would somehow have to know what name it
picked. Unless perhaps you had a Star Cluster list which had all of your
created Star System objects, each with their own list of Planets which you
could use list to call upon maybe
 I have a general grasp on the idea of nesting and calling upon objects
which you don't know the name of, but this goes far beyond my level of
understanding. Can anyone shed some light on how this would work, or
perhaps point me in the right direction of some documentation on this?
Thanks for the help, and I hope this is not too difficult of a question.

Greg
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Class Nesting

2012-02-06 Thread Dave Angel

On 02/06/2012 08:17 PM, Greg Nielsen wrote:

Hello List,

  My name is Greg, and while working on a project I've come across a
rather interesting problem. I'm trying to create a rough model of a star
cluster and all of the stars and planets contained within. Kind of a cool
project; hopefully it should work a little like this. I create a Star
Cluster object, which goes through a list of positions and decides if it
should build a Star System there. If it does, it then creates a Star System
object at that position which in turn calls upon and creates several Planet
objects to reside inside of it. All in all, about 64 positions to check, on
average 24 Star Systems, each with between 2 and 9 planets.
  So here is the problem, to create an object, you need to assign it to
a variable, and you need to know what that variable is to call upon it
later, so to have a object build a second object, it would need to somehow
create a variable name, and you would somehow have to know what name it
picked. Unless perhaps you had a Star Cluster list which had all of your
created Star System objects, each with their own list of Planets which you
could use list to call upon maybe
  I have a general grasp on the idea of nesting and calling upon objects
which you don't know the name of, but this goes far beyond my level of
understanding. Can anyone shed some light on how this would work, or
perhaps point me in the right direction of some documentation on this?
Thanks for the help, and I hope this is not too difficult of a question.

Greg



Since you talk of creating a StarCluster object, presumably you know how 
to make a class.  So in the class definition, you can define attributes 
that each instance has.  One of those attributes can be a list.  So the 
list has a name, but not the individual items in the list.


Generally, it's best to create an empty list attribute in the 
initializer of the class.  Then whatever class method wants to create 
these items can simply append them to the list.


At this point, you should write some code, and it'll either work, or 
you'll tell us what part of it you can't understand.


--

DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Class Nesting

2012-02-06 Thread Steven D'Aprano
On Mon, Feb 06, 2012 at 08:17:05PM -0500, Greg Nielsen wrote:
[...]
  So here is the problem, to create an object, you need to assign it to
 a variable, and you need to know what that variable is to call upon it
 later, so to have a object build a second object, it would need to somehow
 create a variable name, and you would somehow have to know what name it
 picked. Unless perhaps you had a Star Cluster list which had all of your
 created Star System objects, each with their own list of Planets which you
 could use list to call upon maybe

Yes, that's exactly the way to do it. Rather than assigning each object to 
a name:

cluster1 = StarCluster()
cluster2 = StarCluster()
...


you can work with a list of clusters:

clusters = [StarCluster(), StarCluster(), ...]


You can then operate on then one at a time. Say you want to do something to
the 3rd cluster. Remembering that Python starts counting positions at zero,
you would write something like:

clusters[2].name = Local Cluster 12345  # give the cluster a name

If your StarCluster objects are mutable (and if you don't know what that 
means, don't worry about it, by default all classes are mutable), you can 
grab a temporary reference to a cluster while working on it:

for cluster in clusters:  # work on each one sequentially
if cluster.stars == []:
print(Cluster %s has no stars. % cluster.name)

Here I have assumed that each cluster is given a list of stars. Something 
like this:

class StarCluster(object):
def __init__(self):
self.name = no name yet
self.stars = []
def add_star(self, *args, **kw_args):
self.stars.append(Star(*args, **kw_args))


Here I have given the StarCluster a method, add_star, which takes an 
arbitrary 
set of arguments, passes them on to the Star class, and adds the resultant 
star to the list.

class Star(object):
def __init__(self, name=no name yet, kind=red giant, planets=None):
if planets is None:
planets = []
self.planets = planets
self.name = name
self.kind = kind


sol = Star(
Sol, yellow dwarf, 
['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 
 'Uranus', 'Neptune']  # ha ha, Pluto can bite me
)


I hope this helps,



-- 
Steven

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor