how to reuse class deinitions?

James Stroud jstroud at mbi.ucla.edu
Sun Oct 1 19:04:35 EDT 2006


sam wrote:
> hello all,
> 
> pretty straightforward question here, i think. i'm experimenting with
> classes and have written a File class that looks like it's worth
> keeping for future use. however, i cannot work out where to put it for
> this purpose or how to get it in.
> 
> i figured i'd try a bit of (un)inspired guesswork by putting it in my
> module folder, appending sys.path to tell the interpreter where this
> was, and importing it like a module. probably don't need to tell you
> that that didn't work, but i'm drawing a blank in the tutorial and not
> getting any luck in the archives here. maybe i'm just not searching
> with the right words. it's late here and my brain's switching off...
> 
> cutting and pasting it into the interpreter is a bit of a bore. any
> help?
> 
> much appreciated in advance,
> 
> sam
> 
> PS i've just reached first base with classes, and i think i'm starting
> to see what the big deal is about OOP. it's like someone's turned a
> light on.
> 

Here's how I do it. This seems to work pretty well for me. More seasoned 
programmers may have better ways.

1. Develop modules (packages) in a folder called something
    like "~/Code".

2. Name the directory the same name as the module or package.

3. See this for packages and modules how-to:

    http://docs.python.org/tut/node8.html

    You basically will want to think in terms of packages if you use
    my method.

4. Now, set up your $PYTHONPATH environment variable to point at
    the "~/Code" directory. Here's how you do it in UNIX (ask a DOS
    guru for how to do it DOS). This if for your ~/.tcshrc file or
    ~/.cshrc file (whichever you use):

        setenv PYTHONPATH /path/to/Code

    This is for a ~/.bashrc file:

        PYTHONPATH=/path/to/Code
        export PYTHONPATH

    If you have already set $PYTHONPATH somewhere else, then you probably
    don't need me to tell you how to include "/path/to/Code" in it.

5. Open a new shell so the $PYTHONPATH gets set correctly for your rc
    file.

6. Now, start python in that shell and import your packages.

Congratulations! You have now setup an environment where all the code
you write becomes packages and re-usable. Why a similar recipe isn't in 
every tutorial or beginning python book, I haven't a clue because it 
saves hella time figuring out exactly what you have asked here.

James

-- 
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/



More information about the Python-list mailing list