472,328 Members | 1,420 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

how to reuse class deinitions?

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
4 1509
sam
should read 'definitions', of course. i hate stupid typos.

Oct 1 '06 #2
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: DPfan | last post by:
What's exactly the meaning of "code reuse" in C++? Why such kind of reuse have more advantages over the counterpart in other language like in C?...
9
by: cryptic_blade | last post by:
I am working on the design for a new application and want to make the program fully object oriented. I have built and used C++ classes before, but...
3
by: Giovanni Bajo | last post by:
Hello, what's the magic needed to reuse the base-class implementation of a classmethod? class A(object): @classmethod def foo(cls, a,b): #...
0
by: integragreg | last post by:
I apologize in advance if I am posting to the wrong group, but at least one of my questions is related to Platform Invoke in C#. I am using .NET...
3
by: Simon | last post by:
Hi all, I'm hoping that some of you clever chaps could offer me some advice on code reuse. You see, whenever I make applications, I...
8
by: Edward Diener | last post by:
By reuse, I mean a function in an assembly which is called in another assembly. By a mixed-mode function I mean a function whose signature has...
16
by: BartlebyScrivener | last post by:
I am a mere hobbyist. Spent several hours trying to make a class, because I think this is an occasion where I need one. But I can't make it work. ...
6
by: Bart Simpson | last post by:
I remember reading on parashift recently, that "Composition is for code reuse, inheritance is for flexibility" see...
7
by: RichB | last post by:
I am just trying to get to grips with C# and OOP, and one of the benefits would seem to be code reuse. However I am not sure where to draw the line....
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.