[Python-de] Globaler "prefix" o.ae.

Ole Streicher ole-usenet-spam at gmx.net
Di Sep 13 09:55:43 CEST 2011


Thomas 'PointedEars' Lahn <PointedEars at web.de> writes:
> Eine mögliche Lösung habe ich doch schon erklärt.  Für diejenigen, denen das 
> jetzt immer noch nicht klar ist: [...]

Du könntest Dein unangebracht arrogantes Gehabe durchaus lassen.

> -[mypackage/__init__.py]----------------------------------------------
> [...]
>     my_path = os.path.dirname(__file__)
>     if not my_path in sys.path:
>         sys.path.insert(1, my_path)

Das funktioniert nicht, wie schon an anderer Stelle im Thread
diskutiert. Bau doch mal Dein Paket und ein identisches unter
"otherpackage" (d.h., otherpackage.__init__.py ist identisch zu
mypackage.__init__.py).

Dann:
mypackage/common/Base.py, otherpackage/common/Base.py (identisch)
--------------------8<-------------
class Base(object):
    def __str__(self):
        return __file__
--------------------8<-------------

mypackage/database/Databse.py, otherpackage/database/Databse.py (identisch)
--------------------8<-------------
from common.Base import Base
class Database(Base):
    pass
--------------------8<-------------

test.py:
--------------------8<-------------
from mypkg.database.Database import Database
from otherpkg.database.Database import Database as Otherbase

ob = Otherbase()
db = Database()
print ob, db
--------------------8<-------------

zeigt, dass otherpkg.database.Database die falsche Baseklasse nimmt.

> Diese Lösung ist natürlich nicht besonders schön; besser wäre sauberer 
> Quelltext im Originalpaket.  Aber es funktioniert –
>
> $ PYTHONPATH=../tmp python test.py 
> 42

nicht.

~/poo$ python test.py 
/home/ole/poo/mypkg/common/Base.py /home/ole/poo/mypkg/common/Base.py

> HTH

Score adjusted?


Mehr Informationen über die Mailingliste python-de