473,796 Members | 2,538 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Decorator and Metaclasses Documentation

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.

Aug 21 '05 #1
10 1577
Amusingly, I was just perusing these links earlier today. Go go Firefox
history search!

http://www.python.org/2.2/descrintro.html
http://users.rcn.com/python/download/Descriptor.htm

Aug 22 '05 #2
On Sun, 21 Aug 2005 17:01:13 -0700, Jeffrey E. Forcier wrote:
Amusingly, I was just perusing these links earlier today. Go go Firefox
history search!

http://www.python.org/2.2/descrintro.html
http://users.rcn.com/python/download/Descriptor.htm

Are descriptors the same thing as decorators?
--
A wise man knows he knows nothing.

Aug 22 '05 #3
sysfault wrote:
Does anyone know of any good documentation on these topics, doesn't look
like the official python tutorial covers them. Thanks in advance.

PyCon 2005, PyCon 2004 and PyGTA November 2004 presentations here are
all on these topics:
http://www.vrplumber.com/programming/

Though the don't go into extreme detail on decorators (they are
basically syntactic sugar for a particular type of descriptor).

HTH,
Mike

--
_______________ _______________ _______________ ___
Mike C. Fletcher
Designer, VR Plumber, Coder
http://www.vrplumber.com
http://blog.vrplumber.com

Aug 22 '05 #4
sysfault wrote:
On Sun, 21 Aug 2005 17:01:13 -0700, Jeffrey E. Forcier wrote:

Amusingly, I was just perusing these links earlier today. Go go Firefox
history search!

http://www.python.org/2.2/descrintro.html
http://users.rcn.com/python/download/Descriptor.htm


Are descriptors the same thing as decorators?


No. In brief:

Decorators are a mechanism whereby a function or method can be
transparently wrapped by another function.

Descriptors are used to hook programmed functionality into the basic
access mechanisms of object attributes.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Aug 22 '05 #5
Sorry about that, I misread the original question. However, the
python.org link is still valid as it concerns metaclasses as well as a
handful of other topics.

Aug 22 '05 #6
There are also my lectures at Oxford:

http://www.reportlab.org/~andy/accu2...rsofpython.zip

Michele Simionato

Aug 22 '05 #7
Mike C. Fletcher wrote:
(snip)
Though the don't go into extreme detail on decorators (they are
basically syntactic sugar for a particular type of descriptor).

Err... Could you elaborate on this ? Decorators are syntactic sugar for
function wrapping, while descriptors are a 'protocol' to hook into
attribute lookup, so I don't really see how one can describe the first
in terms of the second...

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom. gro'.split('@')])"
Aug 22 '05 #8
bruno modulix wrote:
Mike C. Fletcher wrote:
(snip)

Though the don't go into extreme detail on decorators (they are
basically syntactic sugar for a particular type of descriptor).

Err... Could you elaborate on this ? Decorators are syntactic sugar for
function wrapping, while descriptors are a 'protocol' to hook into
attribute lookup, so I don't really see how one can describe the first
in terms of the second...

There are two major types of descriptors, the elven and the dwarven.

* Elven descriptors are things like property, BasicProperty, VRML97
fields, Zope Field Properties, or PEAK's mechanisms. They mediate
access to an instance's attributes, (both setting and getting of
values) and are used primarily for domain modeling.
* Dwarven descriptors are things like staticmethod or classmethod;
function-like things that tend to look like a function and quack
like a function, but have some special property or functionality
attached when accessed as an attribute of a class/instance
(functions themselves return instance methods or class methods
depending on how they are retrieved).

As you'll read in the PyCon 2005 paper pointed to, the whole set
basically grew out of a generalisation of what functions were already
doing (conceptually). Functions are, in short, the proto-descriptor.
That is, objects which control what result is returned by attribute
access (e.g. an instance or unbound instance method). Many (most?)
dwarven descriptors are implemented as simple wrapper functions around
the base function to use the function's already-present descriptor hooks.

Decorators are syntactic sugar for defining dwarven descriptors. In
Python 2.2 and 2.3 a dwarven descriptor was instantiated like this:

def function( cls ):
"""Do something simple with the class"""
function = classmethod( function )

in Python 2.4+, they can be instantiated like this:

@classmethod
def function( cls ):
"""Do something simple with the class"""

technically you *could* return something that's not a descriptor from
the decorator (e.g. classmethod), but it's unlikely you'd do that very
often.

HTH,
Mike

--
_______________ _______________ _______________ ___
Mike C. Fletcher
Designer, VR Plumber, Coder
http://www.vrplumber.com
http://blog.vrplumber.com

Aug 22 '05 #9
Mike C. Fletcher a écrit :
bruno modulix wrote:
Mike C. Fletcher wrote:
(snip)

Though the don't go into extreme detail on decorators (they are
basically syntactic sugar for a particular type of descriptor).
Err... Could you elaborate on this ? Decorators are syntactic sugar for
function wrapping, while descriptors are a 'protocol' to hook into
attribute lookup, so I don't really see how one can describe the first
in terms of the second...

There are two major types of descriptors, the elven and the dwarven.

* Elven descriptors are things like property, BasicProperty, VRML97
fields, Zope Field Properties, or PEAK's mechanisms. They mediate
access to an instance's attributes, (both setting and getting of
values) and are used primarily for domain modeling.
* Dwarven descriptors are things like staticmethod or classmethod;
function-like things that tend to look like a function and quack
like a function, but have some special property or functionality
attached when accessed as an attribute of a class/instance
(functions themselves return instance methods or class methods
depending on how they are retrieved).


Ok, I see...

In fact, while reading your explanations, it reminded me of some
experiments I made recently playing with descriptors, decorators and
metaclasses to use callable objects implementing the descriptor protocol
as instance methods of other objects (I don't know if I'll ever find a
use case for this trick, but what, they're a lot of things in Python I
never thought I could find a use case for at first and that I'd have
hard time living without nowadays...).

HTH,


It did, thanks.

Aug 22 '05 #10

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

Similar topics

4
2072
by: Michael Sparks | last post by:
Anyway... At Europython Guido discussed with everyone the outstanding issue with decorators and there was a clear majority in favour of having them, which was good. From where I was sitting it looked like about 20:20 split on the following syntaxes: 1 def func(arg1, arg2, arg3) : function... 2 def func(arg1, arg2, arg3): function...
7
1534
by: Peter Otten | last post by:
I'm sure they have been mentioned somewhere but here are some more advantages of a decorator keyword (I use "transform"): - The docstring can be moved to the top of the decorator suite. - Simple attributes that don't affect the function's operation directly can be written in the "natural" name = value form. - Though I expect them to be rare like they are in classes today, statements like if __debug__: decorateForDebugging would be...
30
2512
by: Ron_Adam | last post by:
I was having some difficulty figuring out just what was going on with decorators. So after a considerable amount of experimenting I was able to take one apart in a way. It required me to take a closer look at function def's and call's, which is something I tend to take for granted. I'm not sure this is 100%, or if there are other ways to view it, but it seems to make sense when viewed this way. Is there a way to do this same thing...
22
2242
by: Ron_Adam | last post by:
Hi, Thanks again for all the helping me understand the details of decorators. I put together a class to create decorators that could make them a lot easier to use. It still has a few glitches in it that needs to be addressed. (1) The test for the 'function' object needs to not test for a string but an object type instead.
19
2288
by: Kay Schluehr | last post by:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691
13
1785
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.
11
1833
by: glen.coates.bigworld | last post by:
I'm developing a library at the moment that involves many classes, some of which have "exposed" capabilities. I'm trying to design a nice interface for both exposing those capabilities, and inspecting instances to find out what capabilities they have. At the moment, I'm leaning towards a superclass (Exposed) that defines a static method which is a decorator (expose) such that any derived class can mark a method with @Exposed.expose and...
9
1439
by: Raymond Hettinger | last post by:
I had an idea but no time to think it through. Perhaps the under-under name mangling trick can be replaced (in Py3.0) with a suitably designed decorator. Your challenge is to write the decorator. Any trick in the book (metaclasses, descriptors, etc) is fair game. Raymond -------- how we currently localize method access with name mangling
25
1455
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. passes messages along to those classes) however hand-coding this type of class is rather tedious, so I decided to use decorator for that. Can somebody tell me if what I'm doing is a potential shot-in-the-foot or am I on the right track? (Note, It's...
0
9684
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9530
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10236
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10182
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6793
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5445
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5577
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4120
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.