Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Dics and lambdas

Does anyone know why this doesn't work
PHP Code

<?php 
strat_map 
= {
  
'1': lambda: print('One'),
  
'2': lambda: print('Two')
  
}

strat_map['1']
strat_map['2'] 
but this does
PHP Code

<?php 
strat_map 
= {
  
'1': lambda a: print('One'),
  
'2': lambda a: print('Two')
  
}

strat_map['1']('')
strat_map['2']('') 
¸„»°'´¸„»°'´Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est ¸„»°'´`'°«„¸`'°«„¸¸„»°'´
This is the equivalent of what's happening in the first example, but replace foo with strat_map['1'] or strat_map['2'] in your case:
Code
def foo():
  print('One')

foo

See the problem here? foo, strat_map['1'], and strat_map['2'] all map to function names, but if you want to call them you'll need to use parentheses: foo(), strat_map['1'](), strat_map['2'](), even though they take no arguments.

Your second example does just that.
(Sep 17, 2017 05:24 AM)7key Wrote:  This is the equivalent of what's happening in the first example, but replace foo with strat_map['1'] or strat_map['2'] in your case:
Code
def foo():
  print('One')

foo

See the problem here? foo, strat_map['1'], and strat_map['2'] all map to function names, but if you want to call them you'll need to use parentheses: foo(), strat_map['1'](), strat_map['2'](), even though they take no arguments.

Your second example does just that.

that makes sense thanks :)
¸„»°'´¸„»°'´Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est ¸„»°'´`'°«„¸`'°«„¸¸„»°'´


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Java] by g4143Jan 1, 2017 10:50 AM 0 163 Jan 1, 2017 10:50 AM
Last Post: g4143
[Python] by NosuchredJul 14, 2016 10:28 AM 2 199 Jul 14, 2016 10:28 AM
Last Post: Goodies
[Java] by SIM-2015410Sep 5, 2015 05:01 AM 8 452 Sep 5, 2015 05:01 AM
Last Post: +Haze