Are the critiques in "All the things I hate about Python" valid?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Mon Feb 19 10:15:19 EST 2018


On Mon, 19 Feb 2018 14:06:36 +0100, Anders Wegge Keller wrote:

> Array is not even close to providing a strongly typed container.

That's a mighty powerful claim that goes against the documentation for 
the array module. Can you back your claims up?

Here's an array and a list:

import array
arr = array.array('I')
lst = []


Without cheating (more on this below), your challenge is to do *any* of 
the following (your choice):

1. Change the type of the object currently assigned to arr to 
   a string (or any other non-array type of your choice); OR

2. Change the type of the object currently assigned to lst to
   a string (or any other non-list type of your choice); OR

3. Assign a string (or any other non-integer value of your 
   choice) to any position in the array; OR

4. Assign a negative integer to any position in the array.

If you can do any of those things (with the restrictions given below), 
I'll happily acknowledge that I was wrong, you were right, and Python's 
"strong typing" is much weaker than I thought (or even non-existent).

On the other hand, if you can't do any of them, I expect you to 
acknowledge that you were wrong.


Here are the restrictions ("no cheating"):

- You can't replace, shadow, monkey-patch or otherwise modify 
  the array module or the array.array type.

- Assigning a new object to the variable name "arr" does not 
  count as changing the type of the existing array object. You
  must change the type of the instance, not replace it with
  another instance.

- Likewise for the lst variable name. You must change the type
  of the object, not re-assign a new object to the same name.

- Any sort of hack involving ctypes is interesting, but doesn't
  count; we know that ctypes can break all sorts of invariants
  by manipulating the C engine. Only standard Python code is
  permitted.

- Likewise you aren't allowed to patch the interpreter. It must
  be the standard CPython interpreter, version 2.4 or greater.

- Nor are you allowed to "cheat" by redefining builtins like 
  print, repr, type, id, sys.displayhook, sys.stdout etc in
  order to fake output.

- Exploiting bugs in the interpreter to cause side-effects 
  don't count (its a bug, not a language feature).

- Any other clever trick that goes against the spirit of the
  requirements will be interesting to see, but not count as
  a success.


The spirit of the requirements are that you demonstrate the ability to do 
something like this:

old_lst = lst  # get a second reference to the list object
assert type(lst) is type([])
do_stuff(lst)  # do something clever to the list object...
assert type(lst) is not type([])  # OMG you changed the type!
assert old_lst is lst  # still the same object

(Similar for arr.)

I don't believe that you can do this. I believe that you have failed to 
understand what we're talking about when we say Python has strong, 
dynamic typing, but I'll be happy for you to prove me wrong.



-- 
Steve




More information about the Python-list mailing list