473,386 Members | 1,668 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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.

Dec 12 '06 #1
2 1532

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:
http://docs.python.org/ref/function.html

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

Dec 12 '06 #2
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_name
wrapped
>>print Test2.t2.__doc__
None
>>print Test2.t2.func_defaults
None
>>Test2.t2(100)
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

Dec 12 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Sandy Norton | last post by:
If we are going to be stuck with @decorators for 2.4, then how about using blocks and indentation to elminate repetition and increase readability: Example 1 --------- class Klass: def...
3
by: Ron_Adam | last post by:
Ok... it's works! :) So what do you think? Look at the last stacked example, it process the preprocess's first in forward order, then does the postprocess's in reverse order. Which might be...
2
by: Jeffrey Froman | last post by:
Consider the following class: class Node(object): def __init__(self, name, parent=None): self.name = name self.parent = parent def _ancestors(self, ants=None): if ants is None: ants =
0
by: Eric Huss | last post by:
I'm trying to write a descriptor (similar to the EiffelDescriptor example in the Demo directory). However, I noticed an odd behavior that descriptors written in pure Python mask the underlying...
10
by: sysfault | last post by:
Does anyone know of any good documentation on these topics, doesn't look like the official python tutorial covers them. Thanks in advance. -- A wise man knows he knows nothing.
13
by: Lad | last post by:
I use Python 2.3. I have heard about decorators in Python 2.4. What is the decorator useful for? Thanks for reply L.
3
by: Gary Stephenson | last post by:
I want to define a class-level attribute that will be shared by all subclasses. That is, I want this class, every subclass of this class, every instance of this class and every instance of every...
25
by: samjnaa | last post by:
Please check for sanity and approve for posting at python-dev. In Visual Basic there is the keyword "with" which allows an object- name to be declared as governing the following statements. For...
25
by: Dmitry S. Makovey | last post by:
Hi, after hearing a lot about decorators and never actually using one I have decided to give it a try. My particular usecase is that I have class that acts as a proxy to other classes (i.e....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.