Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Object help

2 views
Skip to first unread message

killsto

unread,
Jan 11, 2009, 5:06:22 PM1/11/09
to
I have a class called ball. The members are things like position,
size, active. So each ball is an object.

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?

Chris Rebert

unread,
Jan 11, 2009, 5:15:01 PM1/11/09
to killsto, pytho...@python.org
On Sun, Jan 11, 2009 at 2:06 PM, killsto <kili...@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.

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

--
Follow the path of the Iguana...
http://rebertia.com

Steven D'Aprano

unread,
Jan 11, 2009, 5:20:17 PM1/11/09
to


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

killsto

unread,
Jan 11, 2009, 6:49:27 PM1/11/09
to
On Jan 11, 2:20 pm, Steven D'Aprano <st...@REMOVE-THIS-

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.

James Mills

unread,
Jan 11, 2009, 6:53:52 PM1/11/09
to killsto, pytho...@python.org
On Mon, Jan 12, 2009 at 9:49 AM, killsto <kili...@gmail.com> wrote:
> Thanks. That makes sense. It helps a lot. Although, you spelled color
> wrong :P.

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

Chris Rebert

unread,
Jan 11, 2009, 7:00:57 PM1/11/09
to killsto, pytho...@python.org
On Sun, Jan 11, 2009 at 3:49 PM, killsto <kili...@gmail.com> wrote:
> On Jan 11, 2:20 pm, Steven D'Aprano <st...@REMOVE-THIS-
> cybersource.com.au> wrote:
>> On Sun, 11 Jan 2009 14:06:22 -0800, killsto wrote:
>> > I have a class called ball. The members are things like position, size,
>> > active. So each ball is an object.
>>
>> > 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.
<snip>

>> This is the TOTALLY wrong approach.
>>
>> Instead of having named balls, have a list of balls.
<snip>

> Just curious, is there another way? How would I do this in c++ which
> is listless IIRC.

If *I* recall correctly, the STL has a `vector` type which is the
equivalent of Python's `list`.

Cheers,
Chris

Terry Reedy

unread,
Jan 11, 2009, 7:44:09 PM1/11/09
to pytho...@python.org
killsto wrote:

> 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).

John Machin

unread,
Jan 11, 2009, 8:07:30 PM1/11/09
to
On Jan 12, 10:49 am, killsto <kilian...@gmail.com> wrote:

> On Jan 11, 2:20 pm, Steven D'Aprano <st...@REMOVE-THIS-cybersource.com.au> wrote:
> > On Sun, 11 Jan 2009 14:06:22 -0800, killsto wrote:
> > > I have a class called ball. The members are things like position, size,
> > > active. So each ball is an object.
>
> > > 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.
>
> > Instead of having named balls, have a list of balls.

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

killsto

unread,
Jan 11, 2009, 11:26:57 PM1/11/09
to
>
> > 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.

James Mills

unread,
Jan 11, 2009, 11:34:02 PM1/11/09
to killsto, pytho...@python.org
On Mon, Jan 12, 2009 at 2:26 PM, killsto <kili...@gmail.com> wrote:
> 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

Steve Holden

unread,
Jan 12, 2009, 3:24:41 AM1/12/09
to pytho...@python.org
James Mills wrote:
> On Mon, Jan 12, 2009 at 9:49 AM, killsto <kili...@gmail.com> wrote:
>> Thanks. That makes sense. It helps a lot. Although, you spelled color
>> wrong :P.
>
> color
> colour
>
> They are both correct depending on what
> country you come from :)
>
They are also both incorrect, depending which country you come from :P

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

0 new messages