Are the critiques in "All the things I hate about Python" valid?
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Sun Feb 18 23:39:31 EST 2018
On Mon, 19 Feb 2018 04:26:32 +0100, Anders Wegge Keller wrote:
> På Mon, 19 Feb 2018 08:47:14 +1100
> Tim Delaney <timothy.c.delaney at gmail.com> skrev:
>> On 18 February 2018 at 22:55, Anders Wegge Keller <wegge at wegge.dk>
>> wrote:
>
>
>> > That list is not only weakly typed, but rather untyped. There are no
>> > safeguards in the language, that enforce that all elements in a list
>> > or other container are in fact of the same type. Before type
>> > annotations and mypy, I could not enforce that other than at runtime.
>
>> You couldn't have got the above much more wrong.
>
>> As others have said, typing is about how the underlying memory is
>> treated.
>
> And that is exactly my point. Python does not have a typed list. It
> have a list that takes whatever is thrown into it.
>
> I'll skip the rest, as you totally missed the point.
I think its actually you have totally missed the point. What you want is
a homogeneous list, a list which only accepts items of the same type. The
built-in list is not that data structure, but Python already has
something similar: see the array module.
If you want a list type which only supports items of a single type, you
can get one by subclassing.
But that's utterly irrelevant to the question of strong versus weak
typing. A strongly typed language like Haskell, which does (almost?) no
automatic coercions at all (possibly not even int to float) can still
define a "list of Any object" type, just like Python's built-in list.
That doesn't make Haskell untyped, and it is a sign of your confusion
that you think it makes Python untyped. Sorry to be blunt, but you're not
even in the right ballpark here.
Python is strongly typed because a list is a list, regardless of its
contents. If you say "1 + []" Python does not try to cast or convert the
list into an int and add them, or interpret the int as a list, and
perform list concatenation on a chunk of memory that is actually an int
but is being treated as an (almost certainly invalid!) list.
Remember:
- the static/dynamic distinction is between *when* types are checked
(compile time or run time), not *whether* they are checked;
- untyped languages treat *everything* (or almost everything) as the same
type: usually either a chunk of raw bits, like in assembly, or text
strings, like in many shell scripting languages;
- weakly typed languages do a lot of automatic coercions;
- strongly typed languages do very few or no automatic coercions.
None of those distinctions have anything to say about whether the
standard library offers a homogeneous list where all the items have the
same type.
--
Steve
More information about the Python-list
mailing list