AW: [Python-de] dynamisch Methoden aufrufen
--- Simon Pamies <lists@bipbap.de> schrieb: > >
name = "two" c = C()
c.__dict__[name]()
Simon
hi
habe ich getestet, funzt aber nicht.
Kann auch nicht gehen, da die Methoden nicht im __dict__ der Ausprägungen stehen, sondern in dem der Klasse. Dabei musst Du darauf achten, dass dort die ungebundene Methode steht, Du also die Ausprägung explizit als Parameter übergeben musst! So geht's: C.__dict__[name](c) Ciao, Rainer Rainer Fischbach Senior Consultant ECS Engineering Consulting & Solutions GmbH Mühlstraße 3 D-92318 Neumarkt Fon +49 9181 4764-84 Fax +49 9181 4764-50 email fischbach@ecs-gmbh.de http://www.ecs-international.de/ _______________________________________________ Python-de maillist - Python-de@python.net http://python.net/mailman/listinfo/python-de
Rainer Fischbach wrote:
Kann auch nicht gehen, da die Methoden nicht im __dict__ der Ausprägungen stehen, sondern in dem der Klasse. Dabei musst Du darauf achten, dass dort die ungebundene Methode steht, Du also die Ausprägung explizit als Parameter übergeben musst! So geht's:
C.__dict__[name](c)
Damit fällt die Vererbung dann unter den Tisch, da die Methoden der Superklassen nicht im __dict__ einer Klasse auftauchen. Dies gilt auch für: getattr(c, name)(c) Folgendes wird funktionieren: getattr(C, name)(c) besser wäre: getattr(c.__class__, name)(c) da dann nur die Instanz benötigt wird und nicht die Klasse selber. Dann wird's aber so langsam unleserlich. Die sauberste Lösung ist, der (Basis-)Klasse eine Methode call_by_name zu verpassen, die den Aufruf der gewünschten Methode übernimmt und gegebenenfalls AttributeError wirft: Beispiel: ------------------------------------- class A: def one(self, *args): print "one called from a %s instance with:" % self.__class__.__name__, args def call_by_name(self, name, *args): getattr(self, name)(*args) # Innerhalb der Klasse geht's mit self class B(A): def two(self, a, b, c): print "two called from a %s instance with:" % self.__class__.__name__, a, b, c class C(B): def three(self): print "three called from a %s instance" % self.__class__.__name__ a = A() b = B() c = C() a.call_by_name("one", "Hello", "World") b.call_by_name("one") b.call_by_name("two", 1, 2, 3) c.call_by_name("one", [], None, {}) c.call_by_name("two", 4, 5, 6) c.call_by_name("three") try: c.call_by_name("four") except AttributeError, args: print "Expected AtributeError:", args ------------------------------------- Ergebnis: ------------------------------------- one called from a A instance with: ('Hello', 'World') one called from a B instance with: () two called from a B instance with: 1 2 3 one called from a C instance with: ([], None, {}) two called from a C instance with: 4 5 6 three called from a C instance Expected AtributeError: C instance has no attribute 'four' ------------------------------------- Ciao, Ulli _______________________________________________ Python-de maillist - Python-de@python.net http://python.net/mailman/listinfo/python-de
Ulrich Berning wrote:
------------------------------------- class A: def one(self, *args): print "one called from a %s instance with:" % self.__class__.__name__, args
def call_by_name(self, name, *args): getattr(self, name)(*args) # Innerhalb der Klasse geht's mit self class B(A): def two(self, a, b, c): print "two called from a %s instance with:" % self.__class__.__name__, a, b, c class C(B): def three(self): print "three called from a %s instance" % self.__class__.__name__ a = A() b = B() c = C()
a.call_by_name("one", "Hello", "World")
b.call_by_name("one") b.call_by_name("two", 1, 2, 3)
c.call_by_name("one", [], None, {}) c.call_by_name("two", 4, 5, 6) c.call_by_name("three")
try: c.call_by_name("four") except AttributeError, args: print "Expected AtributeError:", args
Die Einrückung ist ein wenig durcheinander geraten, Sorry. Ihr werdet's trotzdem verstehen :-) Ciao, Ulli _______________________________________________ Python-de maillist - Python-de@python.net http://python.net/mailman/listinfo/python-de
participants (2)
-
Rainer Fischbach -
ulrich.berning@t-online.de