943,650 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 4187
  • Python RSS
Apr 25th, 2009
0

import classes from a seperate file

Expand Post »
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:

Python Syntax (Toggle Plain Text)
  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:

Python Syntax (Toggle Plain Text)
  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!
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
besktrap is offline Offline
58 posts
since Jul 2008
Apr 25th, 2009
0

Re: import classes from a seperate file

Doesn't spriteObj = sprite_class.Sprite() work?
Featured Poster
Reputation Points: 975
Solved Threads: 140
Posting Virtuoso
scru is offline Offline
1,624 posts
since Feb 2007
Apr 25th, 2009
0

Re: import classes from a seperate file

Quote ...
# 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.
Reputation Points: 741
Solved Threads: 691
Nearly a Posting Maven
woooee is offline Offline
2,302 posts
since Dec 2006
Apr 25th, 2009
0

Re: import classes from a seperate file

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
python Syntax (Toggle Plain Text)
  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.
Reputation Points: 45
Solved Threads: 14
Junior Poster in Training
vidaj is offline Offline
66 posts
since Jul 2007
Apr 25th, 2009
0

Re: import classes from a seperate file

thanks everyone!
I got it working just fine. all I needed was the __init__.py file and some code clean up!
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
besktrap is offline Offline
58 posts
since Jul 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: capturing screen
Next Thread in Python Forum Timeline: Function Return Manipulation





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC