3 ms·
If objects are represented as dictionaries, how do you do a type check, like this object obj is a Window (and not something else, like a Font, or Image)? Test f
by kazinator 5y ago
If objects are represented as dictionaries, how do you do a type check, like this object obj is a Window (and not something else, like a Font, or Image)? Test for the presence of 17 properties that are the telltale marks of a Window?
Why have function parameters and local variables? Everything can be a dictionary. There can be an args and locals which are built-in sybmols. When a function executes, you access keys in args to get it arguments, and keys in locals to work with local variables. For more nuance, there can be fixargs which is a vector and keyargs which is a dict. If you want a new local variable, you just insert a new key into locals.
- kazinator 5y agoWe can stick an ad-hoc type convention, e.g. every dictionary used as an object has to have a "type" key and so forth. Doable, but not too appetizing.
- wheelerof4te 5y agoIn a way, almost everything in Python is a dictionary. But I see your point. API design would be tricky to do without classes, but what is stopping us from making typed dictionaries? If I'm not mistaken, something similar already exists in stdlib. Anyway, my intention was not to argue future design of Python. I just believe that writting classes complicates the overal program design, leading to unpythonic code.
- kazinator 5y agoYes, there is a really stupid thing you can do in Python, which is to add a property to an existing object, which is then available via obj.name syntax. It's not declared in a class or anything.