switch case in python doesn't work; need another pattern -


i need code here, wanted implement switch case pattern in python tutorial said , can use dictionary here problem:

  # type can either create or update or ..    message = { 'create':msg(some_data),               'update':msg(other_data)               # can have more             }    return message(type) 

but it's not working me because some_data or other_data can none (it raise error if it's none) , msg function need simple (i don't want put condition in it).

the problem here function msg() executed in each time filling dict unlike switch case pattern in other programming language don't execute code in case unless it's match.

is there other way or need if elif ...

update: thank replies, it's more

message = { 'create': "blabla %s" % msg(some_data),             'update': "blabla %s" % msg(other_data)             'delete': "blabla %s" % diff(other_data, some_data)            } 

so lambda don't work here , not same function called, it's more real switch case need , , maybe have think on other pattern .

message = { 'create':msg(some_data or ''),             'update':msg(other_data or '')             # can have more           } 

better yet, prevent msg being executed fill dict:

message = { 'create':(msg,some_data),             'update':(msg,other_data),             # can have more           } func,data=message[msg_type] func(data) 

and free define more sensible msg function can deal argument equal none:

def msg(data):     if data none: data=''     ... 

Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -