New user's initial thoughts / criticisms of Python

rusi rustompmody at gmail.com
Sat Nov 9 11:30:26 EST 2013


On Saturday, November 9, 2013 9:26:02 PM UTC+5:30, Roy Smith wrote:
> In article rusi wrote:

> > On Saturday, November 9, 2013 6:38:25 PM UTC+5:30, John von Horn wrote:
> > > Another useful tool in the programmer's toolbox
> > > Select DayofWeek
> > > 	case "mon"
> > > 	...
> > > end select
> > You can typically write this in python as a dictionary
> > cases = {"mon": do_mon-action, 
> >          "tue", do_tue_action,
> > :
> > :        
> > }
> > combined with an 'interpreter'
> > cases[DayofWeek]()
> > Some variants:
> > Need a default?
> > cases.get(DayofWeek, do_default_action)()
> > Sometimes nicer to pass some parameters:
> > cases[DayofWeek](some_relevant_context)

> All of the above is true, but a more straight-forward way to emulate a 
> switch/case is with a series of elifs:

> if day_of_week == "mon":
>    print "mondays suck"
> elif day_of_week == "tue":
>    print "at least it's not monday"
> elif day_of_week == "wed":
>    print "humpday!"
> else:
>    print "it's some other day"

> I've done both.  Both are reasonable translations of switch/case logic 
> from other languages.

> The elif chain is more straight-forward to understand, especially for 
> somebody new to the language.  It also can support more complicated 
> selection logic:

Well

print ( {"mon":"mondays suck",
         "tue":"at least it's not monday",
         "wed":"humpday"
        }.get(day_of_week,"its some other day")
      )

may be dense

Separate into named dictionary and its ok (I think!)



More information about the Python-list mailing list