Where to save classes? How to access classes?

2005-10-31 Thread David Mitchell
Hi,

I'm trying to get into the object oriented aspect of Python. If I create 
a custom class (in it's own file), how do I access that class in a 
function in a different file? In Java there's the notion of a CLASSPATH, 
where you can tell the compiler to look for classes. Is there something 
similar to this in Python?

Thanks,

Dave
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Where to save classes? How to access classes?

2005-10-31 Thread jepler
This section of the tutorial discusses the module search path:
http://docs.python.org/tut/node8.html

6.1.1 The Module Search Path

When a module named spam is imported, the interpreter searches for a file 
named
spam.py in the current directory, and then in the list of directories 
specified
by the environment variable PYTHONPATH. This has the same syntax as the 
shell
variable PATH, that is, a list of directory names. When PYTHONPATH is not 
set,
or when the file is not found there, the search continues in an 
installation-
dependent default path; on Unix, this is usually .:/usr/local/lib/python.

Actually, modules are searched in the list of directories given by the 
variable
sys.path which is initialized from the directory containing the input script
(or the current directory), PYTHONPATH and the installation-dependent 
default.
This allows Python programs that know what they're doing to modify or 
replace
the module search path. Note that because the directory containing the 
script
being run is on the search path, it is important that the script not have 
the
same name as a standard module, or Python will attempt to load the script 
as a
module when that module is imported. This will generally be an error. See
section 6.2, ``Standard Modules,'' for more information. 

Jeff


pgpfmXpeuyJhL.pgp
Description: PGP signature
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Where to save classes? How to access classes?

2005-10-31 Thread David Wahler

David Mitchell wrote:
 Hi,

 I'm trying to get into the object oriented aspect of Python. If I create
 a custom class (in it's own file), how do I access that class in a
 function in a different file? In Java there's the notion of a CLASSPATH,
 where you can tell the compiler to look for classes. Is there something
 similar to this in Python?

 Thanks,

 Dave

Python is a little different than Java in this respect. Whereas Java
loads code one public class at a time, Python loads it in modules,
where a module is (generally speaking) one source file.

Say you have a file, foo.py:
###
class Xyz:
def do_stuff(self):
pass

class Abc:
def do_more_stuff(self):
pass
###

Then in another file, inserting the statement import foo will execute
foo.py and make its global variables available as part of an object
called foo. There is no automatic loading by class and package name
like Java does it. Once you've done this, you can access the members
using expressions such as foo.Xyz e.g. foo.Xyz().do_stuff().

As for the search path: sys.path (the path attribute of the sys module)
is a list of directories which are searched, in order, for modules
specified by import. By default this is the current directory
followed by a few standard Python library directories; most of the time
you shouldn't need to change it, at least for simple projects.

Hope this helps,

-- David

-- 
http://mail.python.org/mailman/listinfo/python-list