4 ms·
Strange that the auto-assigned values start at one.
by Tobu 13y ago
Strange that the auto-assigned values start at one.
- w0utert 13y agoNot that strange, actually. Reserving zero allows using it as an 'undefined' enumeration value that plays well with basic Boolean tests, e.g. 'if not animal: ...' and such. Now all I want in Python is a real switch statement...
- Tobu 13y agoIf this was a strongly-typed language I wouldn't want enums to be nullable, but maybe people would use that in Python. Incidentally, this makes me wonder if one could implement something similar for flags (powers of two values, overloading | and &). Though sets of enums might be sufficient.
- ubernostrum 13y agoPython is a strongly-typed language. Perhaps you're thinking of static versus dynamic typing?
- Tobu 13y agoThe only languages I'd consider strongly typed are static typed. Python functions are always 'a -> 'b (any to any), the weakest possible type for a function. Same for variables (as opposed to values), they are all 'a; same for list membership or attribute membership. There is no place in the language with room for a nullable / nonnullable distinction.
- andreasvc 13y agoIn a dynamic language it's the values that are typed (at runtime), not variables, functions, etc. I think your interpretation of weak vs. strong is already covered by static vs. dynamic, whereas the other interpretation of weak typing is about silent coercion of types such as in Javascript and PHP where for example an integer can be concatenated to a string, or in C where a string can be interpreted as an integer with a cast.
- kstrauser 13y agoYou might consider that, but frankly, you'd be wrong. Python is unambiguously strongly (and dynamically) typed.
- ubernostrum 13y agoA statically-typed language is one in which (1) names and values both are typed, and the only legal binding of a value to a name is when the type of the value is compatible with the type of the name; (2) all expressions have types, and only values or names of the appropriate type may be used or returned. Often but not always, some form of source-code processing prior to execution will evaluate all types in the program and determine whether these conditions are met. A dynamically-typed language is one in which only values have types, and there are no restrictions by type on what values may be bound to particular names. Expressions may or may not have defined types, and may or may not check these types (some dynamically-typed languages do allow statements to be made about the types of functions, for example; some use these in a "static-y" way, while others treat them more as hints for runtime optimization). Strong and weak typing are far less precisely defined. The most broadly-used criterion I've personally seen is that in a strongly-typed language, attempting to perform an operation with values whose types are incompatible with the operation will fail immediately, and will not try to implicitly coerce the values to acceptable types for the operation. Python is generally considered strongly typed, and dynamically typed. There is no consensus that these two labels are incompatible, just as there is no consensus that weak and static typing are incompatible (C is often described as being both statically and weakly typed, for example, due to C's casting and conversion facilities).
- cma 13y agoPython accepts: 1 + True == False + 2 It should have to be: 1 + int(True) == int(False) + 2
- ubernostrum 13y agoPython accepts this because bool is a subclass of int, and True and False have defined integer values (1 and 0 respectively). Which means there's no coercion or conversion going on here. For what you think it "should" be, Python would have to break Liskov substitution for subclasses of int.
- cma 13y agoIt shouldn't be a subclass of int. http://arxiv.org/abs/math/9205211 http://arxiv.org/abs/math/9205211
- pekk 13y agoThen it gets fixed, and everyone screams because it's not backward compatible (Python 3).
- cma 13y agoIt should have just been part of Python 3.
- mixmastamyk 13y agoYep, but they didn't aim to or succeed in 'fixing everything' in python3, just the most pressing items. Here's to Python 4!
- ubernostrum 13y agoYou're free to argue that. But the fact that it is, and behaves correctly as a subclass of int, does not make Python be weakly typed. In fact, rather the opposite.