[Tutor] class attribute to initiate more classes
kent37 at tds.net
Sat Oct 31 14:14:05 CET 2009
On Sat, Oct 31, 2009 at 1:01 AM, Vincent Davis <vincent at vincentdavis.net> wrote:
> I have a program that generates many instances of a class with an attribute
> self.x = random.gauss(10, 2). So each instance has a different value for
> self.x. This is what I want. Now I want to make a class that starts my
> program and sets the attributes.
> class people:
> def __init__(self, size)
> self.size = size
> class makepeople:
> def __init__(self, randomsize)
> self.rsize = randomsize
> self.allpeople = 
> def makethem():
> for x in range(1,100):
> p+str(x) = people(self.rsize)
> so what I would like to have work is set the attribute of makepeople so that
> when it is used to make instances of people each will have a different size.
> listofp = makepeople(random.guass(100, 2))
Here you are calling makepeople() and passing it a single random
number that will be used for all people. You need to call
random.gauss() in the loop.
> I would then what listofp.allpeople to be a list of people with different
> sizes. The above code does not work, I don't think it does anyway.
> I hope this makes sense, I am sure there is a term for what I am trying to
> do but I don't know it.
I would just make a function that creates the list of people. There is
no need to put it in a class. For example,
return [people(random.gauss(100,2)) for x in range(1, 100)] # Note
this only makes 99 people
More information about the Tutor