[Tutor] __init__.py
Lee Cullens
leec03273 at mac.com
Mon May 23 03:08:42 CEST 2005
Joseph,
I'm relatively new to Python, but I might be able to help with your
question.
First, think of the convention "self" (or sometimes "me" in other
languages) as a reference to a specific instance derived from a
class. When you reference an inherited class method of an instance,
the specific instance address is automatically passed as the first
argument. There is nothing special about the label "self" by itself
(you could use instance_ref for example), but it is a convention that
makes your code more easily understood. If I've used any
inappropriate Python terminology, maybe someone will correct such :~)
Now your real question. In my limited Python experience so far the
module (file) __init__.py is used with module packages (multiple
directories involved). Let's say you want to use "import
dir1.dir2.mod" because you have structured you package something like:
dir0/
dir1/
__init__.py
dir2/
__init__.py
mod.py
another_mod.py
dir3/
__init__.py
yet_another_mod.py
Two things here:
1) The container directory (dir0) needs to be added to your module
search path, unless it's the directory of the importing module.
2) Each directory named in the package import statement must contain
a file called __init__.py These may be empty files (used by Python
in the package initialization phase) and are not meant to be executed
directly. However you could use such to create a data file, connect
to a database, etc.
Package imports can be used to simplify your PYTHONPATH/.pth search
path settings. If you have cross-directory imports, you might make
such relative to a common root directly.
We'll let the experts take it from there.
Lee C
On May 22, 2005, at 7:54 PM, Joseph Quigley wrote:
> I've seen many (python) "plugins" (some located in site-packages
> [windows
> here]) with the name __init__.py.
> What's their use?
> class foo:
> def __init__:
> print "this starts first"
> def foo1():
> print "this comes later. Init initializes the chain of
> functions in this
> class
>
> Now, i've never used a program (i can't seem to grasp all the self,
> and
> other things in OOP yet) with __init__ but I know what __init__
> does in a
> class, not as a file name.
> I'm asking this out of curiosity, not for help.
> JQ
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
More information about the Tutor
mailing list