473,406 Members | 2,698 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,406 software developers and data experts.

Re: method decorators and more on decorators

En Tue, 29 Jul 2008 08:45:02 -0300, Themis Bourdenas <bo*******@gmail.com>
escribi�:
On Mon, Jul 28, 2008 at 11:12 AM, Gabriel Genellina
<ga*******@yahoo.com.ar>wrote:
>En Sun, 27 Jul 2008 15:26:39 -0300, Themistoklis Bourdenas <
tb******@doc.ic.ac.ukescribió:
On a related note, as the actual instance method of myclass is not foo
but
decorate(foo), why are they called method decorators? This does not
decorate
methods, they decorate functions and eventually the decorated
functions
become methods. The name method decorator sounds a bit misleading to
me.

Where have you found it? I've always seen the expression "function
decorator" or just "decorator", not "method decorator".

well a few occurrences of it can be found in PEP 318
In a very strict sense, I'd say that all those references to "method
decorators" are wrong - because those "def" statements inside a "class"
statement define functions, not methods. In that sense almost every Python
programmer is wrong too: in this example

class X:
@decorator
def foo(self): pass

most (if not all) people would refer to "foo" as "the foo method in class
X" - even if foo is really a function, not a method.
So a less restrictive meaning is usually adopted - and we all consider
"foo" a method, and we're all happy... except in your case, when you just
hit a point where the distinction is relevant.
Perhaps a more careful wording in the PEP would help in those corner cases.
So returning to my original question is there any way I can get the
class
inside decorate()? I guess there is not, but just asking to make sure.

"the class inside decorate"? What do you mean? The type of the x
instance
in an x.foo() call? That should be determined inside the wrapped
function
-when it is actually called-.

Yes, what I was hoping is that it might be possible to get the name of
the
class the function is eventually is going to be bound to, but I guess
that's
not very likely to be possible. I wanted the name well before the
function
is called, actually even before an instance of the class is created.
No, I think it's not possible - not using a function decorator, because
when it is applied, the class doesn't exist yet.
You might use a metaclass (or a class decorator, available on 2.6 and
3.0); iterate over the dictionary of the class about to be created and
process their functions in any way you like.
Anyway,
I 've solved my problem with other means. Just out curiosity though, has
there been any talk about actual method decorators? I guess you can work
around them with the introduction of class decorators or even now with
meta-classes, but you have to wrap methods manually with no syntactic
sugar.
Mmm... methods are ephemeral objects, once the call is completed, the
method usually gets destroyed, so I don't think a true "method decorator"
would be very useful.
If you really want to customize a method, since they are created by the
function type's __get__, a crazy idea would be to inherit from the
function type and return something different in its __get__ - but I've
never done something like that and don't even know if it's possible at
all...

--
Gabriel Genellina

Jul 30 '08 #1
0 1037

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

Similar topics

17
by: daishi | last post by:
For what it's worth: As far as I know, the proposed @decorator syntax will be the first time that two logical lines of python with the same indentation will not be independent of one another....
4
by: Doug Holton | last post by:
First let me say please see the wiki page about python decorators if you haven't already: http://www.python.org/cgi-bin/moinmoin/PythonDecorators I propose (and others have) that built-in...
4
by: RebelGeekz | last post by:
Just my humble opinion: def bar(low,high): meta: accepts(int,int) returns(float) #more code Use a metadata section, no need to introduce new messy symbols, or mangling our beloved visual...
11
by: Arien Malec | last post by:
I've been following the decorator debate with some interest, and it's taken me a reasonably long time to understand what is meant by a decorator. One of the issues is that the Decorator pattern is...
2
by: Guido van Rossum | last post by:
Robert and Python-dev, I've read the J2 proposal up and down several times, pondered all the issues, and slept on it for a night, and I still don't like it enough to accept it. The only reason...
9
by: Bengt Richter | last post by:
;-) We have @deco def foo(): pass as sugar (unless there's an uncaught exception in the decorator) for def foo(): pass foo = deco(foo) The binding of a class name is similar, and class...
7
by: MR | last post by:
Hello All, I have a question about decorators, and I think an illustration would be helpful. Consider the following simple class: #begin code class Foo: def fooDecorator(f): print...
11
by: Helmut Jarausch | last post by:
Hi, are decorators more than just syntactic sugar in python 2.x and what about python 3k ? How can I find out the predefined decorators? Many thanks for your help, Helmut Jarausch
2
by: Andrew West | last post by:
Probably a bit of weird question. I realise decorators shouldn't be executed until the function they are defined with are called, but is there anyway for me to find all the decorates declared in a...
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
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
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,...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.