On Monday, November 11, 2013 7:31:07 PM UTC+5:30, Roy Smith wrote: > > On Saturday, November 9, 2013 10:30:26 AM UTC-6, rusi wrote:
> > > print ( {"mon":"mondays suck", > > > "tue":"at least it's not monday", > > > "wed":"humpday" > > > }.get(day_of_week,"its some other day") > > > ) > Rick Johnson wrote: > > Proper code formatting can do WONDERS for readability! > > d = { > > "mon":"mondays suck", > > "tue":"at least it's not monday", > > "wed":"humpday" > > } > > default = "some other day" > > target = "tue" > > print d.get(target, default) > > target = "blah" > > print d.get(target, default) > I agree that Rick's version is better than rusi's version, but possibly > not for the the reason Rick thinks it is :-) rusi's version has a > "parsing surprise" in it. As a human scans the code, the thought > process goes something like this: Yes I did not like my own version for similar reason: I expect the switch order (classic C) to be 1. expression 2. body 3. default The following does not quite do it but is it better? def switch(val, default, body_dict): return body_dict.get(val,default) day=... switch(day, "something else", {"mon" : "mondays suck", "tue" : "at least it's not monday", "wed" : "humpday" } ) Of course one can flip the body and the default but I find that looks more confusing. -- https://mail.python.org/mailman/listinfo/python-list