问题
This is an academic question, more than a practical one. I'm trying to delve into the foundations of Python, and I wonder: could I implement the staticmethod
and classmethod
decorators in pure Python if I wanted to?
For staticmethod
maybe I could keep a reference to the original function and call it omitting the first argument?
For classmethod
maybe I could keep a reference to the original function and call it passing the type of the first argument, rather than the argument itself? (How would I handle it being called directly on the class?)
Again, it's not that I want to actually implement it in my programs; I want to learn how the language works from it. If it's possible to do I would really appreciate code samples. If not, I would appreciate explanations why it can't be done theoretically. Thanks!
Edit: Thanks for the answer and for the reference to the descriptor protocol! As a means to further extend my understanding of Python I'd like to ask two more things:
Could this be done without the descriptor protocol, for example by implementing the __ call __ special method in our classes?
Does the descriptor protocol work the same way in Python 2.7 and in Python 3.x?
Thanks again!
回答1:
There are examples in the docs:
class StaticMethod(object):
"Emulate PyStaticMethod_Type() in Objects/funcobject.c"
def __init__(self, f):
self.f = f
def __get__(self, obj, objtype=None):
return self.f
And:
class ClassMethod(object):
"Emulate PyClassMethod_Type() in Objects/funcobject.c"
def __init__(self, f):
self.f = f
def __get__(self, obj, klass=None):
if klass is None:
klass = type(obj)
def newfunc(*args):
return self.f(klass, *args)
return newfunc
回答2:
You should accept J.F. Sebastian's answer, since it answers your question directly, but asking this question means you want to know about the descriptor protocol which you can read about here, which is the thing used to implement these and other important Python internals (and can be useful in your own code when used sparingly). property
, and regular ol' bound methods are other things that are written using the descriptor protocol.
You should check it out, it's not that simple when you first read about it, but it'll click.
来源:https://stackoverflow.com/questions/11768752/can-the-staticmethod-and-classmethod-decoraters-be-implemented-in-pure-python