On 13 October 2017 at 13:54, Gregory Ewing <greg.ew...@canterbury.ac.nz> wrote: > Neil Cerutti wrote: >> >> I can tell at a glance if a parameter is expected to be >> modifiable just by looking at the function signature. > > > The question is why doesn't anyone feel the need to be > able to do that for Python functions? Whether a function > modifies things passed to it is just as important to > know in Python as it is in C.
While I don't *really* know, my intuition is that const is important in C for guaranteeing that functions don't mess around with the innards of (conceptually) primitive types. Hence const char *, and const pointers/arrays. More complex types like collections - lists, trees, etc - can't be declared as "const". Well, they can, but typically that doesn't guarantee that the collection isn't changed, just that the pointer to it isn't. So in C, for all practical purposes const signifies that an argument is what in languages like C# would be called a value type - an immutable primitive value. In Python and other higher level languages, primitive types are immutable by default, or to put it another way, basic types are always value types, and constness is part of the definition of the *type* rather than of the name referring to it. So there's no need for an explicit "const" annotation. To put it another way, in C const is a property of the variable being declared, not the value assigned to it. In Python, variables aren't declared, and constness is an inherent property of the value (or its type). One interesting question which this does raise is whether there's a place for "const" in type annotations - I suspect not, because it's either trivial (the type is immutable) or too hard to define (you'd need to recursively know for all methods whether they mutate the value). Paul -- https://mail.python.org/mailman/listinfo/python-list