Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as Python by blah ( 13 years ago )
de p(v):
    print v
 
 
class A(object):
    def __init__(self, name, command=lambda:None):
        self.name = name
        self.comman = command
    def click(self):
        self.comman()
    def __repr__(self):
        return "< %s:%s >" % (self.__class__.__name__, self.name)
 
k = []
for i in ('a', 'b', 'c'):
  k.append( A(i, command=lambda:p(i)) )
 
for a in k:
    a.click()
"""
will print:
c
c
c
"""
#solution
k = []
for i in ('a', 'b', 'c'):
  k.append( A(i, command=lambda arg=i:p(arg)) )
 
for a in k:
    a.click()
 
"""
will print:
a
b
c
"""

 

Revise this Paste

Your Name: Code Language: