Code: Alles auswählen
class KL(object):
print "*** class KL"
#
def neu(self):
print "*** def neu"
#
def foo():
print "*** def foo", "kennt ii=%i"%ii
return "foo "+str(ii)
#
for ii in range(9):
print foo()
KL().neu()
Code: Alles auswählen
*** class KL
*** def neu
*** def foo kennt ii=0
foo 0
*** def foo kennt ii=1
foo 1
*** def foo kennt ii=2
foo 2
*** def foo kennt ii=3
foo 3
*** def foo kennt ii=4
foo 4
*** def foo kennt ii=5
foo 5
*** def foo kennt ii=6
foo 6
*** def foo kennt ii=7
foo 7
*** def foo kennt ii=8
foo 8
#
[ (1) Was geschieht mit dem Befehl 'print "*** class KL"' ] edit: falschmeldung
(2) Woher kennt die Definition von foo den Wert von ii?
(3) Warum wird die Definition von foo immer wieder neu ausgeführt?
#
Ich gehe davon aus, dass die obige Codierung schlecht ist, da Definitionen nur einmal ausgeführt werden sollten, und ich nicht 9 Objekte erstellen sollte, die alle gleich sind.