[Chicago] Now I get it.
William E. S. Clemens
wesclemens at gmail.com
Tue Jun 16 16:40:55 CEST 2015
I have to apologize, when I wrote my response yesterday I picked "map" as a
variable name. There is a class named "map" that is in the default
namespace. It does work but its a really bad practice to overwrite things
in the default namespace.
--
William Clemens
Phone: 847.485.9455
E-mail: wesclemens at gmail.com
On Tue, Jun 16, 2015 at 7:18 AM, Lewit, Douglas <d-lewit at neiu.edu> wrote:
> Hi William,
>
> That's really interesting. I've used "*map*" before, but never used "*map.get(
> )*" before. What does "*map.get( )*" do? Thanks for the feedback!
>
> Doug.
>
> P.S. I agree about those comprehensions. They are definitely very nice!
> But I think Python programmers should be cautious when using them. If you
> overdo the comprehensions, then the code becomes really obscure and
> difficult to trace through. That's just my humble opinion. Your mileage
> may vary.
>
> On Mon, Jun 15, 2015 at 11:29 PM, William E. S. Clemens <
> wesclemens at gmail.com> wrote:
>
>> 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
>>>
>>>
>>
>> _______________________________________________
>> Chicago mailing list
>> Chicago at python.org
>> https://mail.python.org/mailman/listinfo/chicago
>>
>>
>
> _______________________________________________
> 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/20150616/14fc7249/attachment.html>
More information about the Chicago
mailing list