Object help

K

killsto

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?
 
C

Chris Rebert

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)]
 
S

Steven D'Aprano

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.


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

killsto

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.

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.

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

James Mills

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
 
C

Chris Rebert

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
 
T

Terry Reedy

killsto said:
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).
 
J

John Machin

Or some other collection or container of objects (e.g. a dict or a
queue), depending on what you are trying to simulate.
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
 
K

killsto

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

James Mills

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
 
S

Steve Holden

James said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,235
Latest member
Top Crypto Podcasts_

Latest Threads

Top