Humm, no? Unless you mean ("",)
>>> not ""
TrueHumm, no? Unless you mean ("",)
>>> not ""
Trueex:
answers = {}
answers["I exist"] = ""
if answers["I exist"]:
print("a")
does not print. answers = {}
answers["I exist"] = ""
if "I exist" in answers:
print("a")"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.
https://docs.python.org/3/library/stdtypes.html#truth-value-...
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.
> 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):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)
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.
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.
You get that behavior by using dict.get(): answers.get("I exist") returns None, which is falsy.
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.
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.
...
if answers.get('I exist'):
print('a')
Which is why you should always explicitly check for None if that is your intent. if "I exist" in answers:
... >>> answers = {}
>>> if answers["I don't exist"]:
... print("a")
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
if answers["I don't exist"]:
KeyError: "I don't exist"
The method you're trying to use doesn't work anyway: it doesn't matter that it's confusing. You'd have the same problem with the value False.