On 18 February 2018 at 22:55, Anders Wegge Keller <we...@wegge.dk> wrote:
> På Sat, 17 Feb 2018 15:05:34 +1100 > Ben Finney <ben+pyt...@benfinney.id.au> skrev: > > boB Stepp <robertvst...@gmail.com> writes: > > > > He blithely conflates “weakly typed” (Python objects are not weakly, but > > very strongly typed) > > Python is more strongly typed than PHP, but that doesn't really say much. > However, compared to a language like C, there are big holes in the type > safety. > > >>> alist = [1, 'two', ('three', four), 5*'#'] > > 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. I can't comment on PHP typing, as I've actively avoided that language since my first experience with it. C is statically and weakly typed. Variables know their types at compile time (static typing). It is a feature of the language that you can cast any pointer to any chunk of memory to be a pointer to any other type (normally via void *). This is not coercion - it takes the bit pattern of memory of one type and interprets it as the bit pattern for another type, and is weak typing. Python is strongly and dynamically typed. In Python, once you create an object, it remains that type of object, no matter what you do to it*. That makes it strongly typed. Python does not have variables - it instead has names with no type information at compile time. That makes it dynamically typed. In your list example, each element of the list is a name - the element itself doesn't have a type, but the object named by the list does. * In some cases it is possible to change the __class__ of an object, but that can only be done in restricted circumstances and will usually result in runtime exceptions unless you've specifically planned your class hierarchy to do it. The cases where it is possible to change the __class__ do not result in reinterpretation of memory bit patterns. >>> (1.0).__class__ = int Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: __class__ assignment only supported for heap types or ModuleType subclasses In some implementations it is possible to subvert the Python typing system by stepping out of Python code and into (for example) a C extension, but that does not make Python *the language* weakly typed. Tim Delaney -- https://mail.python.org/mailman/listinfo/python-list