473,761 Members | 4,407 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Re: method decorators and more on decorators

En Tue, 29 Jul 2008 08:45:02 -0300, Themis Bourdenas <bo*******@gmai l.com>
escribi�:
On Mon, Jul 28, 2008 at 11:12 AM, Gabriel Genellina
<ga*******@yaho o.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 1058

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

Similar topics

17
1777
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. Previously, when looking at: some_python(code) and_some_more = stuff there was no need to look at the the first line in order to know what
4
1401
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 features have keyword support, like static and class methods. Also, I believe it is more readable if decorators, especially longer ones, are moved to the top of the function body, just like docstrings, instead of coming before the function is even...
4
1488
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 cleanliness of python.
11
1559
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 more like a Unix pipe than it is what PEP 318 calls a decorator. The classic decorator is run-time, rather than define time, and is non-destructive to the base function. The PEP 318 decorator is define time and is a destructive change to the base...
2
1708
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 to accept it would be to pacify the supporters of the proposal, and that just isn't a good enough reason in language design. However, it got pretty darn close! I'm impressed with how the community managed to pull together and face the enormous...
9
1302
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 decorators would seem natural, i.e.,
7
2282
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 "fooDecorator"
11
2102
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
1953
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 file when I import it? Or perhaps anyway to find the decorators by loading the file by other methods (with out simply parsing it by hand). Basically what I'm looking for is a way to, given a python file, look through that file and find all the...
0
9531
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
9345
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
10115
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9905
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
9775
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7332
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5229
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...
3
3456
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.