Live data from Hacker News

Python idioms I wish I'd learned earlier

prooffreaderplus.blogspot.com

161–170 of 174 posts

Re: Python idioms I wish I'd learned earlier

#161

Earlier quoted context omitted.

( = b c)) same number of operators but a few extra parens

Here (>= b c) will return a boolean value, which you're then comparing to a.

Which means it could be done, but the relational operators (any one of which could be the leftmost constituent) have to all be defined as macros which analyze the rest of the expression with alternative evaluation rules.

Re: Python idioms I wish I'd learned earlier

#162
post #104

Earlier quoted context omitted.

I never liked how people in Python use stringWithSpaces.split instead of a list. Just feels wrong somehow. But I've seen it many times so it's probably pythonic

As DaFranker points out, it's just easier to type than ('Hearts', 'Diamonds', 'Spades', 'Clubs') and has less opportunity for typos and syntax errors. If I was concerned about performance I would replace it with a tuple, but it was Good Enough for a quick example.

Sorry, I realised I sounded negative. Nothing against you, it's just my private opinion.

It's not about performance, but about one more incidental step when reading the code. Small cost, but it's there.

Re: Python idioms I wish I'd learned earlier

#163
post #116
post #86

Sincerely, Transforming Code into Beautiful, Idiomatic Python – by Raymond Hettinger... http://youtu.be/OSGv2VnC0go

I was lucky to watch this video while first learning the language. Every beginner (coming from another language) should watch this to understand the idioms of Python.

Ya, me too. It's also a very funny talk. :)

Re: Python idioms I wish I'd learned earlier

#164
post #76

Some comments: 1. Am I the only one that really loves that `print` is a statement and not a function? Call me lazy, but I don't mind not having to type additional parentheses. 5. Dict comprehensions can be dangerous, as keys that appear twice will be silently overridden: elements = [('a', 1), ('b', 2), ('a', 3)] {key: value for key, value in elements} == {'a': 3, 'b': 2} # same happens with the dict() constructor dic…

with the print as a keyword, you can't have modules defining a print function (for example, APIs where print makes sense as a function name ([he]xchat's py support comes to mind))

additionally, if you want to override print in python2, you need to replace the stdout stream with your own inbetween buffer object, which also has the downside of being global

Re: Python idioms I wish I'd learned earlier

#165
post #162

Earlier quoted context omitted.

As DaFranker points out, it's just easier to type than ('Hearts', 'Diamonds', 'Spades', 'Clubs') and has less opportunity for typos and syntax errors. If I was concerned about performance I would replace it with a tuple, but it was Good Enough for a quick example.

Sorry, I realised I sounded negative. Nothing against you, it's just my private opinion. It's not about performance, but about one more incidental step when reading the code. Small cost, but it's there.

One reason I like it is that you're probably going to be reading in the possible values from a stream or arg or config file anyway.

But you have a good point. YAGNI is YAGNI.

Re: Python idioms I wish I'd learned earlier

#166
post #61

Earlier quoted context omitted.

It's not just a tweak to the parser, and it does have to do with the type system, but you're right that it's not about static typing. The issue is that there are languages (like C) where typing is static but weak, so e.g. booleans are also integers and can have integer operations like '>' applied to them. In other words, the problem is that in C True == 1 and 1 > 2 is a valid expression. In Python, which has strong(e…

Yes, you really can just tweak the parser; just (a) don't have a rule allowing comparisons to appear as children of other comparisons, and (b) add a rule that permits chains of comparisons. Types have zilch to do with this. It's 100% a tweak to the parser.

Interestingly, you can do it with types and no parser help: https://gist.github.com/dlthomas/d18d475068f23584d473#file-c...

Obviously, that's not what's going on in Python...

It could be smoother and more generic if Haskell permitted specifying defaulting for non-numeric types. But it does support things like:

    *Main> 2  2  2  2  4  2  4 

Re: Python idioms I wish I'd learned earlier

#167
post #111

Earlier quoted context omitted.

Your defaultdict approach and the dict.get with a default specified is not really equivalent. In the defaultdict case when you encounter a non existing key it adds a new entry with that key into the dict. i.e. your dict will start growing. whereas dic.get with default value keep returning you the default value without touching your dict.

re: "Your defaultdict approach and the dict.get with a default specified is not really equivalent. In the defaultdict case when you encounter a non existing key it adds a new entry with that key into the dict. i.e. your dict will start growing." europa - The dictionary is only modified if you are using a method to modify it. When you are just passively querying it, it's not impacted. class defaultdict(dict): def __in…

You are right about your implementation. I was thinking about the Pythons collections.defaultdict

from collections import defaultdict s = 'mississippi' d = defaultdict(int) for k in s: d[k] += 1 d.items()

[('i', 4), ('p', 2), ('s', 4), ('m', 1)]

Re: Python idioms I wish I'd learned earlier

#168

If you were underwhelmed by this blog post have a look at: Transforming code into Beautiful, Idiomatic Python by Raymond Hettinger at PyCon 2013 https://speakerdeck.com/pyconslides/transforming-code-into-b... and https://www.youtube.com/watch?v=OSGv2VnC0go&noredirect=1 Topics include: 'looping' with iterators to avoid creating new lists, dictionaries, named tuples and more

And, if possible, see every video by Raymond Hettinger. He is a great teacher.

Re: Python idioms I wish I'd learned earlier

#169
post #167

Earlier quoted context omitted.

re: "Your defaultdict approach and the dict.get with a default specified is not really equivalent. In the defaultdict case when you encounter a non existing key it adds a new entry with that key into the dict. i.e. your dict will start growing." europa - The dictionary is only modified if you are using a method to modify it. When you are just passively querying it, it's not impacted. class defaultdict(dict): def __in…

You are right about your implementation. I was thinking about the Pythons collections.defaultdict from collections import defaultdict s = 'mississippi' d = defaultdict(int) for k in s: d[k] += 1 d.items() [('i', 4), ('p', 2), ('s', 4), ('m', 1)]

The difference here - is that you are actually assigning a value to the dictionary element. It's the assignment that's growing the dictionary, not the query.

Re: Python idioms I wish I'd learned earlier

#170

If you were underwhelmed by this blog post have a look at: Transforming code into Beautiful, Idiomatic Python by Raymond Hettinger at PyCon 2013 https://speakerdeck.com/pyconslides/transforming-code-into-b... and https://www.youtube.com/watch?v=OSGv2VnC0go&noredirect=1 Topics include: 'looping' with iterators to avoid creating new lists, dictionaries, named tuples and more

Sorry for the offtopic but why did you add `&noredirect=1` to the end of that youtube url?
Post reply on HN