[Chicago] Now I get it.

William E. S. Clemens wesclemens at gmail.com
Tue Jun 16 06:29:49 CEST 2015


Although Python does lack some classic control flows notably do-while and
switch-case, it offers plenty of other uniq control flows. I often use a
else statement with a for loop. Python also offers list and
dictionary comprehension.

https://docs.python.org/3.5/tutorial/controlflow.html
https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions

I would also suggest that you can easily implement a do-while like such:

while True:
    pass # Do Stuff

    if condition: break # Our do statement


Switch-cases are a little tricky. Most the time a if ... elif ... else
works just fine, although this is a bit limiting if you want to use a fall
through condition. A neat trick that you can do with python is use a
dictionary as a switch-case.

def foo():
   print("foo")

def bar():
   print("bar")

def baz():
   print("default baz")

map = {
    "Blue": foo,
    "Red": bar
}

map.get("Red", baz)() # bar
map.get("Yellow", baz)() # default baz

This has the added advantage that you can dynamically build out
the dictionary map.


--
William Clemens
Phone: 847.485.9455
E-mail: wesclemens at gmail.com

On Mon, Jun 15, 2015 at 10:55 PM, Lewit, Douglas <d-lewit at neiu.edu> wrote:

> if __name__ = "__main__":
>        etc.
>        etc.
>        etc.
>
> Now I get it.  Do you want to just import the methods from your .py file?
> Or do you want to actually run the program directly?  Cool stuff!
>
> Best,
>
> Douglas.
>
> P.S.  Any thoughts on Ruby guys?  I've been messing around with it
> lately.  Sort of Python-like, but it actually offers more constructs than
> Python.  For example, Ruby has a do-while loop and Ruby offers two
> different ways to represent ranges, one with the upper limit included and
> the other with the upper limit excluded--as in Python.  But Ruby has a very
> "Pythonic" feel to it.
>
> _______________________________________________
> Chicago mailing list
> Chicago at python.org
> https://mail.python.org/mailman/listinfo/chicago
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/chicago/attachments/20150615/ef22fc32/attachment.html>


More information about the Chicago mailing list