How to write good Python objects?

Marijan Tadin mtadin66 at yahoo.com
Fri Oct 24 02:01:52 EDT 2003


> One other thing -- I would like to be able to include a statement in
> my program to the effect of: "from my_package import
> my_function_or_class".  Python seems to look for .pyc files in the
> /Lib folder.  By placing a .py file in /Lib, I got it to compile, and
> to be recognized by an import statement.  Is this the right way for
> users to package their own code?  It seems kludgy to me.

AFAIK if you want to import a python module (actually *.py file), you
can place it in /Lib subdirectory of python distribution, but I think
this subdirectory schould better stay reserved for modules from standard
python distribution.
You can put your module in any directory, and then add this subdirectory
to the PYTHONPATH environment variable of your operating system. I've
been doing this for a while, but then I ended with unmanageable cluster
of files distributed somewhere on my computer.
I think the best way is to use distutilities (Distributing Python
Modules link in your python documentation). I've studied it a few days
ago, and it is much easier to use (at least for simple things) than I
had thought before.
There is also a /lib/site-packages directory in your python
distribution, if you put your *.py file in it, you can allways import it
in your application, and it is also intended as directory for
third-party python modules, and distutilities will install your modules
into this subdirectory. I 'd recommend you to use packages (section 6.4
Packages in the tutorial in your python documentation) if you are not
allready doing so, because otherwise you'll probably soon get name
clashes.
If you use distutillities, you get two extras too:
1. You can easily make a Windows installer for your library, and if you
install your library this way, it will be registerd by OS, so if you
want to uninstall your library, you can do it as for any other windows
programm.
2. If you want to compile C extension, the easiest way to do so is with
distutilities. The first time I succeeded to do so, was with help from
some web post by Alex Martelli (maybe it is in Python recepies, but I'm
not sure). Most information how to do this is rather linux oriented.
Actually it is not difficult to do so on Windows (also with Mingw
compiler) but there is, AFAIK, no detailed (from begin to end at one
place) instruction how to do it for dummies (I still feel like one, so
the worst thing that can happen is a hint: "look into your compiler
documentation"), it is rather distributed over the web.

> I have written one nice, self-contained object that contained a DNA
> sequence, and various functions to manipulate the data therein.  My
> wxPython GUI objects, in contrast, are in a state of constant flux.
> Rather than importing and reusing a piece of code, I find myself
> copying the code into my new program and playing with it, just a bit.
> I'm starting to believe that writing a good GUI object, one that you
> can really reuse, is actually quite hard.  Yes, I know that you can
> derive a new object that overrides properties of your old object.
> Should I find myself doing this for every object that I write?

I do not feel competent enough to give much advice here, but I believe
that, if you start to copy and paste your code, you should reconsider
your design. But, on the other hand, if you intend your library to be
used by others, to many classes and inheritance can be cumbersome. I
remember trying to use some Java library, which can do great things, but
in order to do simplest things with it, I schould use dozen classes,
where each of them was e.g. 5-th or 10-th in some inheritance hierarchy,
and you can imagine the fun of finding documentation of the
class-methods I wanted to use. I can not judge if it is OK for complex
library which is intended to be used by professional programmers.

> At the beginning of 2003, I was a frustrated computer user, and lapsed
> programmer, with problems to solve that screamed for programming.
> Thanks to the Python language and community, I am a programmer once
> again.

I am actualy weather forecaster, and I do some programming in my spare
time. I can remember learning C++ for about 6-7 months, and then
learning Java for about 3-4 months, and still not beeing able to open
text file and do some simple processing (which I could have done easily
in Fortran77 before) without looking in a book . The best advice I've
found in the book "Thinking in Java", was to have a look at Python.

Marijan Tadin






More information about the Python-list mailing list