Hack Forums
[Python] Dics and lambdas - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] Dics and lambdas (/showthread.php?tid=5719362)



Dics and lambdas - vorticalbox - Sep 17, 2017

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']('') 



* - 7key - Sep 17, 2017

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.


* - vorticalbox - Sep 17, 2017

(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 :)