Module/package hierarchy and its separation from file structure

Robert Kern robert.kern at gmail.com
Tue Jan 29 14:44:33 EST 2008


Carl Banks wrote:
> On Jan 29, 7:48 am, Peter Schuller <peter.schul... at infidyne.com>
> wrote:
>>> You can also put, in animal/__init__.py:
>>>  from monkey import Monkey
>>> and now you can refer to it as org.lib.animal.Monkey, but keep the
>>> implementation of Monkey class and all related stuff into
>>> .../animal/monkey.py
>> The problem is that we are now back to the identity problem. The class
>> won't actually *BE* org.lib.animal.Monkey.
> 
> The usage is the same; it works in all cases once you redefine
> __module__.  Who cares what it really is?

The inspect module.

[animals]$ ls
animals
[animals]$ rm animals/*.pyc
[animals]$ ls
animals
[animals]$ ls animals
__init__.py  monkey.py
[animals]$ cat animals/monkey.py
class Monkey(object):
     pass
[animals]$ cat animals/__init__.py
from animals.monkey import Monkey
Monkey.__module__ = 'animals'
[animals]$ python
Python 2.5.1 (r251:54869, Apr 18 2007, 22:08:04)
[GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
 >>> from animals import Monkey
 >>> import inspect
 >>> inspect.getsource(Monkey)
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File 
"/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/inspect.py", 
line 629, in getsource
     lines, lnum = getsourcelines(object)
   File 
"/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/inspect.py", 
line 618, in getsourcelines
     lines, lnum = findsource(object)
   File 
"/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/inspect.py", 
line 494, in findsource
     raise IOError('could not find class definition')
IOError: could not find class definition
 >>>

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list