Object help

Jervis Whitley jervisau at gmail.com
Sun Jan 11 23:17:46 CET 2009


On Mon, Jan 12, 2009 at 9:06 AM, killsto <kiliansto at gmail.com> wrote:

>
> 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?
>
>
How about placing the balls in a python list:balls = [Ball() for i in
range(number_of_balls)]
where number_of_balls is the number of balls you want to create.

additional balls can be added to the list:
balls.append(Ball())

now you can access the balls like so:
>>> balls[0]
< __main__.Ball instance at ...>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090112/2e945ffe/attachment.html>


More information about the Python-list mailing list