Object help

Chris Rebert clp2 at rebertia.com
Sun Jan 11 23:15:01 CET 2009

On Sun, Jan 11, 2009 at 2:06 PM, killsto <kiliansto at gmail.com> wrote:
> I have a class called ball. The members are things like position,
> size, active. So each ball is an object.

Class names should use CamelCase, so it should be `Ball`, not `ball`.

> How do I make the object without specifically saying ball1 = ball()?
> Because I don't know how many balls I want; each time it is different.
> The balls are to be thrown in from the outside of the screen. I think
> you get that is enough information.
> This doesn't directly pertain to balls, I have wanted to do something
> like this for many different things but didn't know how.
> I would think something like:
> def newball():
>     x = last_named_ball + 1
>    ball_x = ball(size, etc) # this initializes a new ball
>    return ball_x
> But then that would just name a ball ball_x, not ball_1 or ball_2.
> Is it possible?

Yes, but only using deep dark black magic. Just use a list of Ball
objects instead.

the_balls = [Ball(size, etc) for i in range(number_of_balls)]

Follow the path of the Iguana...

More information about the Python-list mailing list