[Chicago] Now I get it.

David Sutton davidkentsutton at gmail.com
Tue Jun 16 16:07:48 CEST 2015


"That's really interesting.  I've used "*map*" before, but never used
"*map.get(
)*" before.  What does "*map.get( )*" do?  Thanks for the feedback!"


I think you're confusing the 'map' function with the '(hash)map' data type
(also known as dictionary).

If you create a dictionary like so:

foo = {"bar": "baz"}

You can retrieve the value using either foo['bar'] for foo.get('bar'). Now
if you tried to retrieve a value that didn't exist such like foo['qux'] you
would get a KeyError. To avoid running into that key error you can use the
get method and call something like foo.get('qux'), which would return null
because the 'qux' key is not in the dictionary. If you wanted any failed
look up to return something non-null you can provide the default as a
second argument to the get method. eg foo.get('qux', 'my_default').

In the ruby world this is the same as using the '.fetch' method on a Hash.

On Tue, Jun 16, 2015 at 9:00 AM, Hector Rios <labeledloser at gmail.com> wrote:

> Douglas,
>
> I've been alternating between Ruby and Python for some time now and I can
> just say this: each language is great in it's own way.
>
> My personal preference is Ruby, though. Ruby makes more "sense" to me. The
> syntax, the philosophy, the testing -- everything just makes more sense to
> me.
>
> Python is simple in a sense that you can do something one way, where as
> Ruby has many ways of doing things. However, I believe that things can be
> accomplished in a variety of different ways, which is why Ruby feels more
> natural to me.
>
> That said, Python is a beautiful language with many great things going for
> it. I believe that it is a great language for everyone to use, and I
> encourage people to learn it.
>
>
> {
>     "name": "Hector Rios",
>     "title": "Software Developer",
>     "contact": {
>             "linkedin": "hrios10",
>             "gmail": "labeledloser",
>             "site": "http://hectron.github.io/"
>     }
> }
>
> *No trees were killed to send this message, but a large number of
> electrons were terribly inconvenienced.*
>
> 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
>>
>>
>
> _______________________________________________
> 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/97677aad/attachment.html>


More information about the Chicago mailing list