r/Python • • Nov 14 '17

Senior Python Programmers, what tricks do you want to impart to us young guns?

Like basic looping, performance improvement, etc.

1.3k Upvotes

629 comments sorted by

View all comments

Show parent comments

1

u/b1ackcat Nov 15 '17 edited Nov 15 '17

Well I have to disagree.

  • Being more "pythonic" just for the sake of it is idiotic. That point alone just sounds so pretentious I almost stopped reading. Code needs to be readable, extendable, and maintainable. None of those requirements insist upon blindly following some language-specific dogma.

  • The two conditions actually behave differently. Checking the length explicitly will raise an exception for a None object, whereas checking just the object will return false. More often than not, the former behavior is more desirable, since code should generally fail fast.

  • Checking __len__ is not usually O(n). It depends on the implementation of the collection, and most if not all of the standard Python collection classes track the size in an attribute, so it's usually O(1). I'll grant you that this isn't the most obvious behavior, but still, an important distinction.

  • If the object isn't expected to always be a collection (like a float or something), you already have at best, unmaintainable code, and at worst, a bug, if the condition is relying on the assumption that the object is a collection. Again, checking length here catches that issue, checking the object directly doesn't.

Read the PEP for this condition check. Even the author admits this lint check is probably not the best idea/needs to be rethought. It's just not a good warning, at all.

0

u/stevenjd Nov 16 '17

Being more "pythonic" just for the sake of it is idiotic.

That's a ... strange attitude to take. By definition, "pythonic" code is good, idiomatic, well-written code. As an experienced developer, rather than a cowboy, you should know that idiomatic code is good code. Unidiomatic code is like language which uses its own clever slang that nobody else understands. "Clever" code is a code smell: if you write the cleverest code you can, you're not clever enough to debug it. Idiomatic code is its own reward: idiomatic code is readable code, easy to comprehend, and makes maintenance simpler. You should have a good reason for not writing idiomatic code.

I don't know whether you intended it or not, but you effectively said that it is stupid to write code that others can understand.

That point alone just sounds so pretentious I almost stopped reading. Code needs to be readable, extendable, and maintainable.

Right -- and that's what idiomatic, pythonic code is.

None of those requirements insist upon blindly following some language-specific dogma.

"Blindly"?

My very first words in this discussion were "Depends on the context". If you think I'm talking about blindly following any rule, then your reading comprehension is pretty poor.

The two conditions actually behave differently. Checking the length explicitly will raise an exception for a None object, whereas checking just the object will return false. 

Indeed. And that was my point: it's often desirable to accept None or a collection.

More often than not, the former behavior is more desirable, since code should generally fail fast.

I don't know about "more often or not", but I agree that sometimes you do want None to fail. But not always.

Checking __len__ is not usually O(n). It depends on the implementation of the collection, and most if not all of the standard Python collection classes track the size in an attribute, so it's usually O(1). I'll grant you that this isn't the most obvious behavior, but still, an important distinction.

Indeed. I did specify "a collection with an inefficient __len__".

But that's the thing: if you're duck-typing and can accept any sort of collection or sequence, you don't know in advance that you're going to only receive objects with an efficient len. So why rely on this implementation detail? You don't actually care what the length is. You only care whether the collection is empty or not, and the idiomatic way of writing that is if myCollection.

If the object isn't expected to always be a collection (like a float or something), you already have at best, unmaintainable code, and at worst, a bug, if the condition is relying on the assumption that the object is a collection. Again, checking length here catches that issue, checking the object directly doesn't.

Be reasonable: not all code that accepts arbitrary objects is buggy. And chances are that if you pass a float or an int, you'll get an exception just a few lines further in, when you try to call some other sequence or collection method. Besides, that's really an argument against duck-typing. If you want to be sure you have a collection, then call isinstance, or check for the availability of the methods you know you want.

Assuming you've already checked that the object is (let's say) a collection, then there is no extra benefit in calling len when you don't actually care about the length.

Read the PEP for this condition check. Even the author admits this lint check is probably not the best idea/needs to be rethought. It's just not a good warning, at all.

I can't comment on the lint author's opinion, but PEP 8 mandates the if myCollection idiom for the standard library. In your own code, of course you can write anything you like, no matter how pointless:

if ((sum(1 for x in myCollection) == 0) is True) is True: ...

wink

0

u/b1ackcat Nov 16 '17

By definition, "pythonic" code is good, idiomatic, well-written code.

You're taking an untrue statement and treating it as a factually true one, then basing your entire argument on it.

Based on the rest of your comment history, it's clear you have an extremely biased opinion on this matter and aren't going to be swayed or even open to others opinions on the matter. Best of luck with that.