On Wed, Dec 27, 2017 at 9:41 PM, Kirill Balunov <kirillbalu...@gmail.com> wrote: > Will there be any implications (or is it possible) if all variables will > have an attribute *something like* `__type__` which by default will be > initialized to *something like* `AnyType`. So in the case `x = 12` will be > equivalent to `x: AnyType = 12`. > > x: int > x = 12 > x.__type__ # int > > a, b = 11, 12 > a.__type__ # AnyType > b.__type__ # AnyType
This won't work. When you say "a.__type__", it means "take the *value* referenced by a, and look up its __type__ attribute". So it's equivalent to writing: (11).__type__ (12).__type__ To query something about the *variable*, you have to look at its enclosing namespace. If these are at top level, you'd be looking at the module-level __annotations__ dictionary. That's also where x.__type__ would be - it's actually stored in __annotations__["x"]. > And also in this case: > > class Dummy: > a: int > b: float > c = [] > > Dummy.__annotations__ # will be {'a': int, 'b': float, 'c': AnyType} Now, this situation might be of some interest to the dataclass discussions. Currently, there's a limitation in that no single source has information about all three attributes; and that means that the order ["a", "b", "c"] is actually not stored anywhere. (Annotations are ordered within themselves; values are ordered within themselves; but annotations and values are separate.) There is definitely room to ask the question "can we get default annotations for any global or class-level name that gets assigned to without an annotation?". I fully expect that thread to be one of those gigantic ones, but have fun :) > While I ask purely out of curiosity, I think this is not an off-topic for > python-list. Absolutely, it's not off-topic! Though at some point it might need to migrate to python-ideas, if there's a proposal to actually change the language. But there's nothing wrong with asking the question here on python-list. ChrisA -- https://mail.python.org/mailman/listinfo/python-list