Jump to content

Python, dics and lambda

vorticalbox

Does anyone know why this doesn't work

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

strat_map['1']
strat_map['2']

but this does

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

strat_map['1']('')
strat_map['2']('')

 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

The first one just returns a function object, the second one actually calls that function object. 

But since you don't use the arguments at all you should just do

 

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

strat_map['1']()
strat_map['2']()

 

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×