| | |
import classes from a seperate file
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
hi everyone! I have two files, main.py and sprite_class.py. sprite_class.py is in a separate folder called lib. here's a quick diagram:
------------------------------------
/folder engine/
main.py
/folder lib/
sprite_class.py
------------------------------------
...sprite_class.py contains a class called Sprite:
since i'm kind of rusty when it comes to import statements, I created a small def. function to handle it for me. also at the bottom, I import my sprite_class.py file:
Now, the only problem I have is trying to run the class in main.py. I know how to run def. statements, but how do you run classes?
Any hints?
Thank you in advanced!
------------------------------------
/folder engine/
main.py
/folder lib/
sprite_class.py
------------------------------------
...sprite_class.py contains a class called Sprite:
Python Syntax (Toggle Plain Text)
# sprite class class Sprite(): def __init__( self, start_x, start_y, image_path ): self.x = start_x self.y = start_y self.starting_y = self.y self.image_path = image_path self.sprite = pygame.image.load( self.image_path )
since i'm kind of rusty when it comes to import statements, I created a small def. function to handle it for me. also at the bottom, I import my sprite_class.py file:
Python Syntax (Toggle Plain Text)
def CD( Dir_String ): sys.path.append( Dir_String ) # changes the system's current dir. path to specified path CD( "/lib" ) import sprite_class
Now, the only problem I have is trying to run the class in main.py. I know how to run def. statements, but how do you run classes?
Any hints?
Thank you in advanced!
•
•
Join Date: Dec 2006
Posts: 1,028
Reputation:
Solved Threads: 290
•
•
•
•
# changes the system's current dir. path to specified path
sys.path.append("/engine/lib/") ## or whatever it is
import sprite_class
and then
sprite_obj = sprite_class.Sprite()
to call a function in the class Sprite, use
sprite_obj.function_name(vars)
to access a variable
print sprite_obj.starting_y
Note that the function CD does not return anything so sprite_obj will be destroyed when you exit the function.
•
•
Join Date: Jul 2007
Posts: 66
Reputation:
Solved Threads: 14
First off, to include a file in a different folder, you don't have to change your current working directory. A folder in python is called a package, and the file in that folder is called a module. To correctly create a package, you have to create a file called __init__.py inside that folder.
When you have created that file, all you have to do is
If you have a programming background from java or a similair language, it's the usual practice to have each class in a separate file. In python you don't do that. You have similair classes in one module (i.e. one file) and you structure your modules in packages (i.e. folders). It's usually not a good idea to bundle each class in a separate module inside a folder (package) called lib. It's counter-intuivite. Think about which classes goes together, and bundle those in the same module.
Example from standard python library (version 3.0)
there's a package called html. That package has a module called client. The client-module has classes and functions that are related to the html-client.
There's a package called urllib. That package has a module called parse. The parse module has classes and functions that are related to parsing urls.
If you manage to structure your code like this, and try to keep each module separate, i.e. you don't have inter-dependencies in your module, your code will (probably) be easier to understand and easier to maintain.
When you have created that file, all you have to do is
python Syntax (Toggle Plain Text)
from lib.sprite_class import Sprite mySpriteObject = Sprite()
If you have a programming background from java or a similair language, it's the usual practice to have each class in a separate file. In python you don't do that. You have similair classes in one module (i.e. one file) and you structure your modules in packages (i.e. folders). It's usually not a good idea to bundle each class in a separate module inside a folder (package) called lib. It's counter-intuivite. Think about which classes goes together, and bundle those in the same module.
Example from standard python library (version 3.0)
there's a package called html. That package has a module called client. The client-module has classes and functions that are related to the html-client.
There's a package called urllib. That package has a module called parse. The parse module has classes and functions that are related to parsing urls.
If you manage to structure your code like this, and try to keep each module separate, i.e. you don't have inter-dependencies in your module, your code will (probably) be easier to understand and easier to maintain.
![]() |
Similar Threads
- Java Gui help (Java)
- Random Salary (Java)
- how do I pass values between classes? (Python)
Other Threads in the Python Forum
- Previous Thread: capturing screen
- Next Thread: Function Return Manipulation
| Thread Tools | Search this Thread |
alarm app beginner cipher cmd cx-freeze data decimals development dictionary directory dynamic error examples feet file float format ftp function generator getvalue gui halp homework http images import input ip itunes java keycontrol leftmouse line linux list lists logging loop maintain maze millimeter module mouse mysqldb number numbers output parsing path port prime programming projects push py2exe pygame pyglet pyqt python queue random recursion schedule screensaverloopinactive script scrolledtext slicenotation sqlite ssh string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode url urllib urllib2 variable variables ventrilo verify vigenere web webservice wikipedia windows wxpython xlwt






