Okay so in Python the PEP8 standard promotes checking for an empty string like the following:

string = ""
not string (which evaluates to True)

But what if I want to check whether a string has content?

string = "hi"
not not string

:awesome:

#Python #Programming

@arkedos I suppose this is a joke, but if you actually want to convert a string to its raw boolean, you can use `bool(string)`.

In most cases, you don't need to do that because it's done implicitly, e.g.

if string:
...

@pganssle Yeah it was a joke, I was simply amused by the fact that you could use double negation to achieve that. :D

Follow

@arkedos Yeah, I wasn't sure mainly becuase that's actually a fairly common idiom for converting to bool in some languages, actually. I've definitely seen !!x around in C and C++.

According to this random blog post, it shows up more than 5000 times in the Linux kernel: dev-notes.eu/2019/10/Double-Ne

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.