Hacker News .hnnew | past | comments | ask | show | jobs | submitlogin

It's not a problem? The async interface isn't a problem either. It's just a thing you have to remember about python: "most input is truthy except for the input that isn't"

"Most of the time you don't disrupt your program by not keeping the returned reference in scope except for when you do"

It's just a thing that trips people up.



Truthy is a Pythonic core principle of the language. It is not an edge case phenomenon in the language which I would expect a regular practitioner to confuse.

https://docs.python.org/3/library/stdtypes.html#truth-value-...


I mean, I've seen bugs around that in code I've worked on and I've created bugs where it's a factor.

Weakrefs are also a core part of the language: https://docs.python.org/3/library/weakref.html . You can't use python without using them.


What I learned when I wrote Python professionally was “never rely on truthiness” explicitly writing out a boolean expression that does what you want is more explicit (“explicit is better than implicit”, PEP 8) and prevents a whole class of bugs down the line.


PEP 8, which you mention, explicitly recommends relying on truthiness:

> For sequences, (strings, lists, tuples), use the fact that empty sequences are false:

  # Correct:
  if not seq:
  if seq:

  # Wrong:
  if len(seq):
  if not len(seq):


PEP8 is touted a lot as if it is a perfectly correct tome of ... correctness. I've worked in Python long enough to know that it both doesn't cover everything and the advice is sometimes actively bad.


Amen

I mean, I'm not blaming PEP8 per se (A Foolish Consistency is the Hobgoblin of Little Minds) but it has a tendency to be taken as gospel by a lot of people

Funny enough, those who push for more strict adherence are also the ones that neglect other aspects (speed, alg. complexity, etc) especially readability (and no, PEP8 compliant code is not necessarily the most readable)


> "most input is truthy except for the input that isn't"

Can you think of any other value that has len(x) == 0 but is truthy?

It’s quite simple. Empty collections and zeros are false, almost everything else is true.

The real head scratcher is that midnight is false.


The alternative here (which would make your example work) is that dictionary lookups return some falsy sentinel value when the key does not exist, just like in javascript. In javascript, this has made a lot of people very angry and been widely regarded as a bad move.

Those values tend to propagate out and make a mess, sneaking into your stored data or causing errors in unexpected places -- fixing this feels like one of the main advantages of using things like typescript, and it's just not an issue in python.

It's not a problem, because the alternative is way worse. It's just a different language design than the one you're used to.

You could argue about whether or not truthiness makes sense as a concept (personally I think not), but the way it's defined in python is quite coherent and useful in practice.


> The alternative here (which would make your example work) is that dictionary lookups return some falsy sentinel value when the key does not exist, just like in javascript. In javascript, this has made a lot of people very angry and been widely regarded as a bad move.

You get that behavior by using dict.get(): answers.get("I exist") returns None, which is falsy.


Right, sometimes you want that. I suppose my point was that the definition of truthiness works well with the default, most obvious way of indexing a dictionary, which was the original complaint.

Perhaps I was a bit harsh -- that exposes an issue which does trip people up, where they use `if x:` when you mean `if x is not None:`.

Saying that, I think it's defensible in the same way. The fact that you can write `if x:` (where x is not a bool) tells you that the language has a concept of truthiness, and so maybe you should have a think about what that actually means.


> It's just a thing you have to remember ...

The more of these things there are, the more brainpower you devote to remembering the right way to do things; if you don't you introduce bugs, a subtle, painful one here.


"Empty containers are falsy" is a Python fundamental, this isn't a subtle bug, but an obvious one.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: