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?
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.
Example:
the_balls = [Ball(size, etc) for i in range(number_of_balls)]
--
Follow the path of the Iguana...
http://rebertia.com
This is the TOTALLY wrong approach.
Instead of having named balls, have a list of balls.
balls = [] # no balls yet
balls.append(Ball()) # one ball comes in from off-screen
balls.append(Ball()) # and a second
del balls[0] # the first ball got stuck in a tree
balls = [] # all the balls were swept up in a hurricane and lost
balls = [Ball(), Ball(), Ball(), Ball()] # four balls come in
balls.append(Ball()) # and a fifth
for b in balls:
print b.colour # print the colour of each ball
and so forth.
--
Steven
Thanks. That makes sense. It helps a lot. Although, you spelled color
wrong :P.
Just curious, is there another way? How would I do this in c++ which
is listless IIRC.
color
colour
They are both correct depending on what
country you come from :)
> Just curious, is there another way? How would I do this in c++ which
> is listless IIRC.
Why are you asking us ? We're Python devs/programmers
not C++
cheers
James
PS: I wouldn't touch C++ with a 10-foot pole
If *I* recall correctly, the STL has a `vector` type which is the
equivalent of Python's `list`.
Cheers,
Chris
> Just curious, is there another way? How would I do this in c++ which
> is listless IIRC.
If you do not have 0) built-in expandable arrays, as in Python, one can
1) program (or find) the equivalent of Python lists;
2) use linked-lists (as long as one does not need O(1) random access);
3) pick a maximum number of items, either for the app or for the run,
and allocate space for that.
Python enthusiasts include those who see the virtue of option 0).
Or some other collection or container of objects (e.g. a dict or a
queue), depending on what you are trying to simulate.
> > for b in balls:
> > print b.colour # print the colour of each ball
>
> Thanks. That makes sense. It helps a lot. Although, you spelled color
> wrong :P.
At this time of day you are likely to find yourself communicating with
Australians. Get used to it :-)
Cheers,
John
I was kidding. IMO, we Americans should spell color like everyone
else. Heck, use the metric system too while we are at it.
Yes well why don't you start up a rally and convince
your brand new shiny government to catch up with
the rest of the world! :)
cheers
James
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/