tonybaloney,
@tonybaloney@fosstodon.org avatar

Darn it Python, why??

>>> bool("False")
True
>>> bool("false")
True
>>> bool("FALSE")
True
>>> bool("0")
True

kytta,
@kytta@fosstodon.org avatar

@tonybaloney because otherwise:

username = request.form.get("username", None)
if not username:
return "Bad Request", 400

Full Name: John False
Username: false

Registration failed: 400 Bad Request

tonybaloney,
@tonybaloney@fosstodon.org avatar

@kytta I understand the truthiness of types, but for some reason I thought bool() would have some logic for friendly string-to-bool checks.

kytta,
@kytta@fosstodon.org avatar

@tonybaloney sounds like a hack, but would json.loads("false") not be kind of a "friendly" string-to-bool conversion? 😂

tonybaloney,
@tonybaloney@fosstodon.org avatar

@kytta I prefer that over eval()

cliffordheath,
@cliffordheath@mastodon.social avatar

@tonybaloney that should have been four exceptions l

stfn,
@stfn@fosstodon.org avatar

@cliffordheath @tonybaloney why do you think so?

cliffordheath,
@cliffordheath@mastodon.social avatar

@stfn @tonybaloney Type conversion is not the same thing as validating user input. It must always succeed, or throw an exception. No unrecognised or imprecise input should succeed

stfn,
@stfn@fosstodon.org avatar

@cliffordheath @tonybaloney not sure what you mean. The examples above are simple type casting. A non-empty string is a truthy value. Casting a truthy value into a bool gives you True. Simple enough.

cliffordheath,
@cliffordheath@mastodon.social avatar

@stfn @tonybaloney Type conversions are reversible, they round-trip (bijection). A language that doesn't distinguish between down-casting and type conversion is not a safe language. If ("0"!="false") then bool("0") should not = bool("false"). I used to think think otherwise but participated in enough war stories to change my mind!

stfn,
@stfn@fosstodon.org avatar

@cliffordheath @tonybaloney i do not agree. You are talking about different things.

"0" != "False" you are comparing the values of strings, and they are different, so it is True.

bool("0") == bool("False") you are comparing the bool evaluations of two strings. And bool of a non-empty string is true so true == true.

Python has the concept of "truthy" values. Anything apart from False, None, integer 0 and empty string will evaluate to True

cliffordheath,
@cliffordheath@mastodon.social avatar

@stfn @tonybaloney Young padawan, I do understand truthiness, having built my first compiler 45 years ago, and coded every day since, in dozens of languages. This seductive concept is wrong-headed in ways that apparently cannot be taught, but must be learned. I wish you every success

stfn,
@stfn@fosstodon.org avatar

@cliffordheath @tonybaloney I am thankful for your input, it's always good to listen to people with more experience, you gave me a good food for thought

bignose,
@bignose@fosstodon.org avatar

@tonybaloney

Because any non-empty string evaluates true.

>>> if "false":
... print("A non-empty string evaluates true")
... else:
... print("and it'll never evaluate false")
...
A non-empty string evaluates true

chrisjrn,
@chrisjrn@social.coop avatar

@tonybaloney see also: Criticisms of JavaScript?

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