ctypes mystischer dir()-Aufruf

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
droptix
User
Beiträge: 521
Registriert: Donnerstag 13. Oktober 2005, 21:27

Kann mir dieses Verhalten jemand erklären?

Code: Alles auswählen

>>> import ctypes
>>> dir(ctypes.windll)
['LoadLibrary', '__class__', '__delattr__', '__dict__', '__doc__', '__getattr__', 
'__getattribute__', '__getitem__', '__hash__', '__init__', '__module__', 
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', 
'__weakref__', '_dlltype', 'kernel32']
>>> ctypes.windll.user32
<WinDLL 'user32', handle 7e360000 at 14ea450>
>>> dir(ctypes.windll)
['LoadLibrary', '__class__', '__delattr__', '__dict__', '__doc__', '__getattr__', 
'__getattribute__', '__getitem__', '__hash__', '__init__', '__module__', 
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', 
'__weakref__', '_dlltype', 'kernel32', 'user32']
>>> 
Zunächst steht mir laut dir() ctypes.windll.user32 gar nicht zur Verfügung. Erst wenn ich es aus heiterem Himmel mal "anspreche", liefert mir dir(ctypes.windll) plötzlich auch 'user23' ganz hinten dran.

Woher weiß ich denn, was ich da noch so alles ansprechen kann, obwohl es mir dir() nicht verrät?
BlackJack

Ich würde mal darauf tippen, dass `ctypes.windll` versucht die DLL zu laden, wenn man das Attribut anspricht.
Benutzeravatar
birkenfeld
Python-Forum Veteran
Beiträge: 1603
Registriert: Montag 20. März 2006, 15:29
Wohnort: Die aufstrebende Universitätsstadt bei München

Code: Alles auswählen

>>> class C:
...  def __getattr__(self, name):
...   setattr(self, name, 42)
...
>>> c = C()
>>> dir(c)
['__doc__', '__getattr__', '__module__']
>>> c.user32
>>> dir(c)
['__doc__', '__getattr__', '__members__', '__methods__', '__module__', 'user32']
>>> 
Dann lieber noch Vim 7 als Windows 7.

http://pythonic.pocoo.org/
Antworten