[Python-de] dynamisch Methoden aufrufen
Markus Jais
markus_jais at yahoo.de
Mit Nov 12 14:58:37 CET 2003
--- Simon Pamies <lists at bipbap.de> schrieb: > >
> >>name = "two"
> >>c = C()
>
> c.__dict__[name]()
>
> Simon
>
>
hi
habe ich getestet, funzt aber nicht.
#!/usr/bin/env python
class C:
def __init__(self):
pass
def one(self):
print "in one"
def two(self):
print "in two"
name = "two"
c = C()
print c.__dict__
c.__dict__[name]()
Ausgabe:
{}
Traceback (most recent call last):
File "./cal.py", line 21, in ?
c.__dict__[name]()
KeyError: 'two'
__dict__ von c ist also leer.
der andere vorschlag mit getattr funzt.
greift getattr nicht auf auf __dict__ zurück ?
Markus
__________________________________________________________________
Gesendet von Yahoo! Mail - http://mail.yahoo.de
Logos und Klingeltöne fürs Handy bei http://sms.yahoo.de