User: Password:
|
|
Subscribe / Log in / New account

Affects Perl too

Affects Perl too

Posted Mar 14, 2014 19:21 UTC (Fri) by mathstuf (subscriber, #69389)
In reply to: Affects Perl too by intgr
Parent article: A false midnight

> most contexts there is no implicit casting from str to int.

What contexts do?

Anyways, while we're in Python's deeper, darker corners, some things I really dislike about Python:

bool('False') -> True (ast.parse_literal is needed) whereas int('1') -> 1
str.split() is approximately filter(len, split(' ')) (there is no way to pass non-None arguments to str.split and get the no-arg behavior)


(Log in to post comments)

Affects Perl too

Posted Mar 17, 2014 12:13 UTC (Mon) by intgr (subscriber, #39733) [Link]

> What contexts do?

Actually I can't think of any from the top of my head. I just said "most" because I suspect there are a few edge cases in the standard library; Python's typing isn't always as strict as people make it out to be. Implicit casts to str are quite common, for example.

> bool('False') -> True (ast.parse_literal is needed) whereas int('1') -> 1

Why do you think this should return False? It doesn't work this way for most other built-in types either, consider list('[1,2]'). The fact that it works for numeric types is a coincidence, because that's a useful way to represent numbers. It never was a goal for sometype(str(value_of_sometype)) return the original value again.

I think using bool(x) to get the truth value of x makes lots of sense, just like list(iterable) does.

> there is no way to pass non-None arguments to str.split and get the no-arg behavior

Agreed, that is inconsistent.

Affects Perl too

Posted Mar 17, 2014 13:13 UTC (Mon) by mathstuf (subscriber, #69389) [Link]

The only place I can think of for implicit str casts is Python2's print statement. The %s way warns at least IIRC.

Casts for compound types makes sense since the internal type to parse out is very ambiguous. I would have thought bool would follow int since they're both POD-ish. It bit me in a config parser (it was our pre-existing format, so the stdlib module wasn't going to work) where I passed a function to use to cast the value out from the parse. You could use int, but anything else needed a wrapper function.


Copyright © 2018, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds