Did you know you can enclose a function in a variable, and call it from that variable?
Check this out, all these statements print the greeting:
# from list:
[print][0]('ahoj')
# or:
[print].pop()('ahoj')
# from tuple:
(print,)[0]('ahoj')
# from dictionary:
{ 'a': print }['a']('ahoj')
# or
{ 'a': print }.get('a')('ahoj')
# even from a lambda function:
(lambda : print)()('ahoj')
Level 2:
Now let’s try to use the itemgetter
!
from operator import itemgetter
itemgetter(0)([print])('ahoj') # list
itemgetter(0)((print,))('ahoj') # tuple
itemgetter('a')({'a': print})('ahoj') # dictionary
It works!
Happy coding
MK