Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to reuse class deinitions?

0 views
Skip to first unread message

sam

unread,
Oct 1, 2006, 6:27:52 PM10/1/06
to
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.

sam

unread,
Oct 1, 2006, 6:29:52 PM10/1/06
to
should read 'definitions', of course. i hate stupid typos.

Scott David Daniels

unread,
Oct 1, 2006, 6:48:48 PM10/1/06
to
sam wrote:
> 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.

What I do:

For each new major version of python, in .../site-packages I make a
directory "sdd" (my initials). In it I put an empty file named
"__init__.py". When I have things I want to reuse, I put them in
files named things like ".../site-packages/sdd/goodidea.py", and
I get use of them in python programs like:

from sdd.goodidea import File
...
<code that uses File>
...

or (actually my current style):
from sdd import goodidea
...
<code that uses goodidea.File>
...

--Scott David Daniels
scott....@acm.org

James Stroud

unread,
Oct 1, 2006, 7:04:35 PM10/1/06
to

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/

sam

unread,
Oct 1, 2006, 7:15:03 PM10/1/06
to

> What I do:
>
> For each new major version of python, in .../site-packages I make a
> directory "sdd" (my initials). In it I put an empty file named
> "__init__.py". When I have things I want to reuse, I put them in
> files named things like ".../site-packages/sdd/goodidea.py", and
> I get use of them in python programs like:
>
> from sdd.goodidea import File
> ...
> <code that uses File>
> ...
>
> or (actually my current style):
> from sdd import goodidea
> ...
> <code that uses goodidea.File>
> ...
>

this is basically what i was trying to do. i just tried it again, with
a class_defs.py file in a folder i appended to the system path, itself
containing the class definition for File. then:

from class_defs import File

works fine.

nice to know i was on the right lines. thanks for the pointer!

0 new messages