2 ms·
In python everything is an object. I'm sure there are minor exceptions or arguable cases but that's also the case with Ruby. Most infamously with Proc. Classe
by rat87 2y ago
In python everything is an object.
I'm sure there are minor exceptions or arguable cases but that's also the case with Ruby. Most infamously with Proc.
Classes are objects in python, usually instances of class called type or a subclass of type(usefulfor controlling class creation), this is called the metaclass.
A constructor call MyObject() is basically just the __call__ method of the metaclass.
Modules are also objects usually of type types.ModuleClass or a subclass although really you can stick any object in sys.modules dictionary and have it act like a module and import it.
Sure it doesn't use messages instead attributes whether field or methods both go through the same lookup method __getattribute__ which goes up the class inheritance chain in an OO manner. There's also __getattr__ for more method_missing like access instead of overriding every object attribute access.
A function or lambda is an instance of function class although I don't think you can't modify or override the function class I think you can hook it to log it.
You can also do all sorts of stuff by hooking module import and rewriting the ast. Or more easily you can decorate/wrap the functions in another function object.
It's true that you can't modify c based classes but you can do a lot of magic in python, people usually just don't because unnecessary magic is frowned on. But sometimes it's useful like the way pytest rewrites
assert a==[1,2,3]
to print a nice error message with both variables
https://github.com/pytest-dev/pytest/blob/main/src/_pytest/assertion/rewrite.py https://github.com/pytest-dev/pytest/blob/main/src/_pytest/a...