mCodingLLC / VideosSampleCode

Code from the mCoding sample videos
MIT License
952 stars 198 forks source link

[Question: Method overloading] Overloading does not work with class/static methods? #9

Closed qexat closed 2 years ago

qexat commented 2 years ago

When I try to call an overloaded class/static method, I get an error (TypeError: 'Overload' object is not callable). I guess it's normal behavior, but why would class/static methods not work with overloading?

Script run: overload.py Python version: 3.10.2

mCodingLLC commented 2 years ago

Overload is a descriptor, so when you do a.method() it calls the get method of Overload and then the call of the result of that. Get returns a BoundOverloadDispatcher (which is callable) if it was called on an instance, but it just returns self (Overload) if it was called on the class itself. This is how most descriptors work because you want to be able to get and set the descriptor itself by getting and setting the attribute on the class object. You could certainly make it work with classmethods and staticmethods, I just didn't do that for the video since it would probably be more work than the rest of the video combined. If you want inspiration for how you might do this, check out the implementation of ABCMeta, which does work with static and class methods.

qexat commented 2 years ago

Oh ok I see, thank you for the quick response! My project does not really require to use staticmethods or classmethods so it is fine for now. But it definitely could be a nice feature, might have a look later