vatbub,

In the future, when people ask me why I dislike #Python, I will redirect them to this #StackOverflow question: https://stackoverflow.com/questions/53513/how-do-i-check-if-a-list-is-empty

A seemingly simple task: Check if a list is empty or not.

Solution 1:
if not a:
print("List is empty")

Pros:

  • Very concise
  • According to the comments "idiomatic"
  • Apparently always O(1)

Cons:

  • How the hell can a list suddenly be boolean!?!?!?
vatbub,

Solution 2:
if len(a) == 0:
print('the list is empty')

Pros:

  • Any human being instantly understands the purpose of this code.

Cons:

  • "This is anti PEP 8"
  • Too verbose
  • "ugly and unpythonic"
  • Misleading (for whatever reason?)
  • Not O(1) in some rare cases
vatbub,

(Cons continued)

  • "It's like showing your friend a container full of 0 oranges. It's not a container of oranges... and if it doesn't contain anything... then it could be argued to be a container at all. It only becomes a container containing contents once contents are contained."
tshirtman,
@tshirtman@mas.to avatar

@vatbub all languages have some conceptual simplifications, some rules you can rely on, but still need to learn at first, in python, "empty containers are 'falsy'" is one of them, it works with [] but also, (), {}, "", and people creating a new container type are expected to make it work the same. Using that rule is "pythonic", because it uses that python feature to make code clearer (to a python developer, who also know that rule).

vatbub,

@tshirtman I understand that, because in fact, my favourite programming language (Kotlin) has some quirks which some programmes who come from a different language find disturbing, but I got accustomed to. My main issue with this pythonic syntax is that I use strongly typed languages for most of my work and my brain therefore tripped over the fact that a list suddenly becomes a boolean. I know however that for people who use Python on a daily basis, this might be more obvious.

tshirtman,
@tshirtman@mas.to avatar

@vatbub Python is strongly typed, the list doesn't become a boolean (although i see why you'd see it this way), it's just that when evaluated for "truthiness", it is falsy, it's a well defined thing, but yes, not obvious.

Python definitely has quirks, if you want real reasons to dislike it, imho the mutable default argument issue is a much nastier trap, also, how closures work tend to drive people nut the first time (at least…) they try to use lambda in a loop, e.g to create callbacks.

tshirtman,
@tshirtman@mas.to avatar

@vatbub there is a very interesting list of puzzling behavior here, if you want to become an expert about python weirdness (though most of them make sense, with some effort).

https://github.com/satwikkansal/wtfpython

tshirtman,
@tshirtman@mas.to avatar

@vatbub (i've been doing mostly python for nearly 20 years, and quite like it, though i'm still on the look for a better non-niche language, rust might be it, as it seems to be quickly getting mainstream adoption).

  • All
  • Subscribed
  • Moderated
  • Favorites
  • python
  • DreamBathrooms
  • thenastyranch
  • osvaldo12
  • magazineikmin
  • GTA5RPClips
  • rosin
  • InstantRegret
  • Youngstown
  • slotface
  • ngwrru68w68
  • kavyap
  • khanakhh
  • Durango
  • mdbf
  • JUstTest
  • everett
  • ethstaker
  • normalnudes
  • cisconetworking
  • tacticalgear
  • cubers
  • modclub
  • provamag3
  • Leos
  • tester
  • anitta
  • megavids
  • lostlight
  • All magazines