[Tutor] Class Nesting

Dave Angel d at davea.name
Tue Feb 7 03:45:31 CET 2012


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



More information about the Tutor mailing list