Cross-reference 'import' in a class hierarchy
Marcus Alanen
marcus at infa.abo.fi
Tue Apr 8 07:12:07 EDT 2003
>> I continue to be amazed that there's simply no way to say
>>
>> import "../foo/ook.py"
>>
>> as should be possible to implement any well-architected class hierarchy.
>
> It's simple. Code should not depend directly on code higher than it in
>the package hierarchy. If it does (and it isn't one of a few extremely rare
>cases), it is misdesigned.
>
> Code shallow in the hierarchy should manage required references to objects
>required by code deep in the hierarchy by passing them around explicitly.
>Any other solution is just bunk.
Could you explain why using import via PYTHONPATH is different than
import via current directory?
E.g define a class in file foobar/t2.py:
class A:
pass
And then, use it from foobar/test.py:
from foobar import t2 as t1
import t2
print id(t1.A)
print id(t2.A)
Why are the id:s different? That is, why do I get two copies of the A
class? This leads (IMHO) to the fact that one _never_ should use
"import X", (witness isinstance(t1.A(), t2.A) returning 0...)
everything should go via PYTHONPATH. Which in turn means that
you have to set PYTHONPATH for all programs. ?
Some insight into why this is done as it is would be helpful.
--
Marcus Alanen
maalanen at abo.fi
More information about the Python-list
mailing list