By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,427 Members | 1,354 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,427 IT Pros & Developers. It's quick & easy.

how to reuse class deinitions?

P: n/a
sam
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.

Oct 1 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
sam
should read 'definitions', of course. i hate stupid typos.

Oct 1 '06 #2

P: n/a
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
sc***********@acm.org
Oct 1 '06 #3

P: n/a
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/
Oct 1 '06 #4

P: n/a
sam

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!

Oct 1 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.