Re: [Python-de] packages und __init__.py
Hallo,
Meine Problem ist ein anderes: wenn __init__.py leer ist, dann steht in der Applikation: from <package>.Interfaces import Container
Das dokumentiert, dass Container im Modul Interfaces ist. Wenn aber __init__.py die Zeile enthält: from Interfaces import Container dann steht in der Applikation: from <package> import Container
Bei fremdem Code wird man vermuten, es gibt ein Modul Container.py. Wenn dies nicht der Fall ist, muss ich in __init__.py nachsehen, was dort definiert ist.
Das finde ich etwas umständlich bzw. verwirrend.
Das ist Ansichtsache. Wenn der Modulentwickler aus Gruenden des Handlings halt verschiedene Files angelegt hat, aber eben so tun will als ob die Elemente eine Ebene hoeher verankert sind, dann macht er das halt so - es hindert dich ja keiner dran, den anderen import zu verwenden. Mir geht es eher auf den Zeiger wenn ich aus einem Modul noch Untermodule importieren muss. Mit __all__ (wie du selber schon feststelltest) hat das aber garnix zu tun. MfG Diez _______________________________________________ python-de maillist - python-de@python.net http://python.net/mailman/listinfo/python-de
Hi, folgende Funktion macht nicht das, was ich eigentlich gerne hätte: def fun(): locals()["x"] = 42 print locals() print x Beim Aufruf zeigt das erste print an, daß die Variable x mit Wert 42 in locals() vorkommt. "print x" allerdings liefert eine Fehlermeldung "NameError: global name 'x' is not defined" Hab ich da was falsch verstanden ??? Gruß, Uwe _______________________________________________ python-de maillist - python-de@python.net http://python.net/mailman/listinfo/python-de
Hi Am 25.08.2004 10:49:57 schrieb Uwe Schmitt:
Hi, folgende Funktion macht nicht das, was ich eigentlich gerne hätte:
"print x" allerdings liefert eine Fehlermeldung "NameError: global name 'x' is not defined"
Der globale Name x existiert ja auch nicht, x ist lokal. Wenn du in einer Funktion nur lesend auf eine Variable zugreifst, nimmt Python an, dass x global ist. Nur wenn du in der Funktion in die Variable schreibst, nimmt Python an, dass sie lokal ist. Wenn die Variable auch bei schreibenden Zugriff global sein soll, muss am Anfang der Funktion "global x" stehen. Scheinbar interpretiert Python locals()['x']=42 nicht als schreibenden Zugriff. Was hat das ganze eigentlich fuern Sinn? cu boesi -- #1671 : icq-intern Frueher hattet ihr die Freiheit zu entscheiden #73628288 : icq-extern Heute seid ihr von dieser Entscheidung befreit boesi111 : aim .-==Report der Magd==-. i171 : reallife _______________________________________________ python-de maillist - python-de@python.net http://python.net/mailman/listinfo/python-de
Moin, die Python Doku sagt ganz klar, dass man locals() nicht verwenden sollte, um die Variablen zu verändern gerade weil die Änderungen nicht unbedingt "übernommen" werden: | locals() - | Update and return a dictionary representing the current local symbol table. | Warning: The contents of this dictionary should not be modified; | changes may *not* affect the values of local variables used by the interpreter.""" Gruss Oliver _______________________________________________ python-de maillist - python-de@python.net http://python.net/mailman/listinfo/python-de
Uwe Schmitt wrote:
Hi, folgende Funktion macht nicht das, was ich eigentlich gerne hätte:
def fun(): locals()["x"] = 42 print locals() print x
Beim Aufruf zeigt das erste print an, daß die Variable x mit Wert 42 in locals() vorkommt.
"print x" allerdings liefert eine Fehlermeldung "NameError: global name 'x' is not defined"
Hab ich da was falsch verstanden ???
Hast du die Dokumentation zu locals() gelesen? http://www.python.org/doc/2.3.4/lib/built-in-funcs.html locals() Update and return a dictionary representing the current local symbol table. Warning: The contents of this dictionary should not be modified; changes may not affect the values of local variables used by the interpreter. -schorsch -- Georg Mischler -- simulations developer -- schorsch at schorsch com +schorsch.com+ -- lighting design tools -- http://www.schorsch.com/ _______________________________________________ python-de maillist - python-de@python.net http://python.net/mailman/listinfo/python-de
Hast du die Dokumentation zu locals() gelesen?
http://www.python.org/doc/2.3.4/lib/built-in-funcs.html
locals() Update and return a dictionary representing the current local symbol table. Warning: The contents of this dictionary should not be modified; changes may not affect the values of local variables used by the interpreter.
Also, ich hab mal auf python.org gesucht, aber auf die Schnelle nix gefunden. Danke an alle, die mir geantwortet haben. Gruß, Uwe _______________________________________________ python-de maillist - python-de@python.net http://python.net/mailman/listinfo/python-de
participants (5)
-
Alexander 'boesi' Bösecke
-
Diez B. Roggisch
-
Georg Mischler
-
Oliver Horn
-
Uwe Schmitt