יואב שלו
יואב שלו

Reputation: 19

How to make argument return his own name?

My code

for i in list_tasks:
    globals()['icon1%s' % i].bind(on_press=lambda x: **print(#here I want to print his name)**

for example globals()['icon1%s' % i] is "icon1clock" I want it to print his own name (icon1clock) on press.

Upvotes: 0

Views: 50

Answers (1)

chepner
chepner

Reputation: 532153

The tricky part is defining a lambda expression that wraps another value, rather than a name. I wouldn't bother trying. Write a function that creates the callback for you; the callback can be a closure over a variable.

def make_callback(name):
    def _(x):
        print(name)
    return _


# *Not* icon1clock = ...
icons = {
    'clock': ..., 
}

for i in list_tasks:
    icons[i].bind(on_press=make_callback(i))

Upvotes: 1

Related Questions