import classes from a seperate file

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jul 2008
Posts: 58
Reputation: besktrap is an unknown quantity at this point 
Solved Threads: 1
besktrap's Avatar
besktrap besktrap is offline Offline
Junior Poster in Training

import classes from a seperate file

 
0
  #1
Apr 25th, 2009
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:

  1. # sprite class
  2.  
  3. class Sprite():
  4. def __init__( self, start_x, start_y, image_path ):
  5. self.x = start_x
  6. self.y = start_y
  7. self.starting_y = self.y
  8. self.image_path = image_path
  9. 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:

  1. def CD( Dir_String ):
  2. sys.path.append( Dir_String )
  3. # changes the system's current dir. path to specified path
  4.  
  5. CD( "/lib" )
  6.  
  7. 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!
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 1,608
Reputation: scru has a spectacular aura about scru has a spectacular aura about 
Solved Threads: 130
Featured Poster
scru's Avatar
scru scru is offline Offline
Posting Virtuoso

Re: import classes from a seperate file

 
0
  #2
Apr 25th, 2009
Doesn't spriteObj = sprite_class.Sprite() work?
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,028
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 290
woooee woooee is offline Offline
Veteran Poster

Re: import classes from a seperate file

 
0
  #3
Apr 25th, 2009
# changes the system's current dir. path to specified path
It doesn't change the path, it adds that path to the list that Python searches. You can print sys.path if you want to see what it is.
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 66
Reputation: vidaj is an unknown quantity at this point 
Solved Threads: 14
vidaj vidaj is offline Offline
Junior Poster in Training

Re: import classes from a seperate file

 
0
  #4
Apr 25th, 2009
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
  1. from lib.sprite_class import Sprite
  2.  
  3. 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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 58
Reputation: besktrap is an unknown quantity at this point 
Solved Threads: 1
besktrap's Avatar
besktrap besktrap is offline Offline
Junior Poster in Training

Re: import classes from a seperate file

 
0
  #5
Apr 25th, 2009
thanks everyone!
I got it working just fine. all I needed was the __init__.py file and some code clean up!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC