Multiple Objects of the same class in Python
I have a bunch of Objects from the same Class in Python.
I've decided to put each object in a different file since it's easier to manage them (If I plan to add more objects or edit them individually) However, I'm not sure how to run through all of them, they are in another Package So if I look at Netbeans I have TopLevel... and there's also a Package named Shapes in Shapes I hav开发者_如何学Pythone Ball.py, Circle.py, Triangle.py (inside the files is a call for a constructor with the details of the specific shape) and they are all from class GraphicalShape That is configured in GraphicalShape.py in the TopLevel Package. Now, I have also on my Toplevel Package a file named newpythonproject.py, which would start the process of calling each shape and doing things with it, how do I run through all of the shapes? also: Is it a good way to do this? p.s. never mind the uppercase lowercase stuff...Just to clarify, I added a picture of the Project Tree
http://i47.tinypic.com/2i1nomw.pngIt seems that you're misunderstanding the Python jargon. The Python term "object" means an actual run-time instance of a class. As far as I can tell, you have "sub-classes" of the Shape
class called ball
, circle
and triangle
. Note that a sub-class is also a class. You are keeping the code for each such sub-class in a separate file, which is fine.
I think you're getting mixed up because you're focusing on the file layout of your project far too early. With Python it is often easier to start with just one file, writing everything you need in that file (functions, classes, etc.). Just get things working first. Later, when you've got working code and you just want to split a part of it into another file for organizational reasons, it will be much more obvious (to you!) how this should be done.
In Python, every class does not have to be defined in its own separate file. You can do this if you like, but it is not compulsory.
it's not clear what you mean when you say "run through them all".
If you mean "import them for use", then you should:
- Make sure the parent folder of
shapes
is on the PYTHONPATH environment variable; then use from shapes import ball
.
精彩评论