Emulate @classmethod using decorator and descriptor

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • WaterWalk

    #1

    Emulate @classmethod using decorator and descriptor

    Hello, I was recently learning python decorator and descriptor and
    emulated a @classmethod decorator:
    class EmuClassMethod( object):
    def __init__(self, f=None):
    self.f = f
    def __get__(self, obj, klass=None):
    if klass is None:
    klass = type(obj)
    def wrapped(*args):
    return self.f(klass, *args)
    return wrapped

    class Test(object):
    @EmuClassMethod
    def t(cls):
    print "I'm %s" % cls.__name__

    It worked, and seems that a function decorator works as follows:
    # decf is a decorator
    @decf
    def func():
    print 'func'

    will be "converted" to:

    def func():
    print 'func'
    func = decf(func)

    Is this really the case? Or correct me if i'm wrong. Thanks in advance.

  • Dustan

    #2
    Re: Emulate @classmethod using decorator and descriptor


    WaterWalk wrote:
    Hello, I was recently learning python decorator and descriptor and
    emulated a @classmethod decorator:
    class EmuClassMethod( object):
    def __init__(self, f=None):
    self.f = f
    def __get__(self, obj, klass=None):
    if klass is None:
    klass = type(obj)
    def wrapped(*args):
    return self.f(klass, *args)
    return wrapped
    >
    class Test(object):
    @EmuClassMethod
    def t(cls):
    print "I'm %s" % cls.__name__
    >
    It worked, and seems that a function decorator works as follows:
    # decf is a decorator
    @decf
    def func():
    print 'func'
    >
    will be "converted" to:
    >
    def func():
    print 'func'
    func = decf(func)
    >
    Is this really the case? Or correct me if i'm wrong. Thanks in advance.
    Yes, the two are equivalent. The documentation is found here:


    But I personally found the explanation for decorators a bit confusing.

    Comment

    • Gabriel Genellina

      #3
      Re: Emulate @classmethod using decorator and descriptor

      On 12 dic, 08:46, "WaterWalk" <toolmas...@163 .comwrote:
      Hello, I was recently learning python decorator and descriptor and
      emulated a @classmethod decorator:
      class EmuClassMethod( object):
      def __init__(self, f=None):
      self.f = f
      def __get__(self, obj, klass=None):
      if klass is None:
      klass = type(obj)
      def wrapped(*args):
      return self.f(klass, *args)
      return wrapped
      >
      class Test(object):
      @EmuClassMethod
      def t(cls):
      print "I'm %s" % cls.__name__
      Basically you're right. But note that @classmethod does some additional
      work to keep the details: the function name is now "wrapped", no
      docstring, no argument names, no defaults, wrong type...
      >>class Test2(object):
      .... @EmuClassMethod
      .... def t2(cls, arg1, arg2=2):
      .... "t2 docstring"
      .... print "I'm %s arg1=%s arg2=%s" % (cls.__name__, arg1, arg2)
      ....
      >>print Test2.t2
      <function wrapped at 0x00AB30B0>
      >>print Test2.t2.func_n ame
      wrapped
      >>print Test2.t2.__doc_ _
      None
      >>print Test2.t2.func_d efaults
      None
      >>Test2.t2(10 0)
      I'm Test2 arg1=100 arg2=2
      >>Test2.t2(arg1 =100)
      Traceback (most recent call last):
      File "<stdin>", line 1, in ?
      TypeError: wrapped() got an unexpected keyword argument 'arg1'

      --
      Gabriel Genellina

      Comment

      Working...