c++ - "case sequence" in python implemented with dictionaries -


i have implemented function:

 def postback(i,user,tval):     """functie ce posteaza raspunsul bazei de date;stringul din mesaj tb sa fie mai mic de 140 de caractere"""     result = {         1:api.postdirectmessage(user,'trebuie sa-mi spui si marca pe care o cauti'),         2:postmarket(user,tval),         3:api.postdirectmessage(user,'imi pare rau, dar nu stiu unde poti gasi aceste tipuri de smantana: %s' % tval)}     return result.get(i) 

but not work case alternative(from c++) executes 3 cases no matter wha try...i'm begginer there might error, please help!p.s. please don't tell me if...else.. alternative cause know can work

it executes 3 cases because define result dict way! call 3 functions , assign them keys 1, 2, 3.

what should instead this:

functions = {     1: lambda: api.postdirectmessage(user,'trebuie sa-mi spui si marca pe care o cauti'),     2: lambda: postmarket(user,tval),     3: lambda: api.postdirectmessage(user,'imi pare rau, dar nu stiu unde poti gasi aceste tipuri de smantana: %s' % tval)} func = functions.get(i) if func:     return func() else:     raise valueerror("bad index: %d!" % i) 

here define small wrapper functions , store in dict instead. pick right function , call on 1 function.


Comments

Popular posts from this blog

windows - Why does Vista not allow creation of shortcuts to "Programs" on a NonAdmin account? Not supposed to install apps from NonAdmin account? -

c++ - How do I get a multi line tooltip in MFC -

unit testing - How to mock PreferenceManager in Android? -