Re: is it possible to see if a class has a decorator ?
On 06-12-2010 16:04, Jean-Michel Pichavant wrote: > Stef Mientki wrote: >> On 06-12-2010 12:08, Ben Finney wrote: >>> Stef Mientki writes: >>> >>> I would like to know if a class definition has a decorator, >>> I'm not sure what this question means. >>> >>> Applying a decorator to a class definition produces a normal class. >>> >>> Classes don't “have” decorators; classes can be returned by a decorator >>> function, but AFAIK the resulting class doesn't “have” the decorator in >>> any sense. >>> >>> is that possible ? >>> The return value of a decorator isn't special in any way, AFAIK. >>> >>> Any function can return a class object or a function object, and any >>> function can be used as a decorator. >>> >>> The only thing that makes a function a decorator is how it is used in >>> the code; but it doesn't leave a trace that I know of. >>> >>> Now, what is it you're trying to do? Perhaps there's a better solution >>> we can come up with. >>> >>> >> Thanks Ben, >> here some more explanation. >> >> I've a number of (dynamic) applications, >> launched from a central wrapper. >> All these modules have a class "Start", which launches the application and >> embeds them in the >> wrapper application. >> >> Module 1: >> class Start (): >> >> >> Module 2: >> @auth >> class Start (): >> ... >> >> When the wrapper application is started, it looks for all dynamic modules >> (without importing them), >> and list these application in a hierarchical tree. >> In the above axmple, >> I would like to know that the class "Start" in Module 2 has the decorator >> "Auth", *without >> importing the module*, >> (so depending on the user logged in, I can decide to add or not add the >> module to the >> hierarchical tree). >> >> thanks, >> Stef Mientki >> >> >> >> > You best bet is to parse the source file. thanks, I was afraid of that. cheers, Stef > > JM -- http://mail.python.org/mailman/listinfo/python-list
Re: is it possible to see if a class has a decorator ?
Stef Mientki wrote: On 06-12-2010 12:08, Ben Finney wrote: Stef Mientki writes: I would like to know if a class definition has a decorator, I'm not sure what this question means. Applying a decorator to a class definition produces a normal class. Classes don't “have” decorators; classes can be returned by a decorator function, but AFAIK the resulting class doesn't “have” the decorator in any sense. is that possible ? The return value of a decorator isn't special in any way, AFAIK. Any function can return a class object or a function object, and any function can be used as a decorator. The only thing that makes a function a decorator is how it is used in the code; but it doesn't leave a trace that I know of. Now, what is it you're trying to do? Perhaps there's a better solution we can come up with. Thanks Ben, here some more explanation. I've a number of (dynamic) applications, launched from a central wrapper. All these modules have a class "Start", which launches the application and embeds them in the wrapper application. Module 1: class Start (): Module 2: @auth class Start (): ... When the wrapper application is started, it looks for all dynamic modules (without importing them), and list these application in a hierarchical tree. In the above axmple, I would like to know that the class "Start" in Module 2 has the decorator "Auth", *without importing the module*, (so depending on the user logged in, I can decide to add or not add the module to the hierarchical tree). thanks, Stef Mientki You best bet is to parse the source file. JM -- http://mail.python.org/mailman/listinfo/python-list
Re: is it possible to see if a class has a decorator ?
On Mon, 06 Dec 2010 22:08:33 +1100, Ben Finney wrote: > Stef Mientki writes: > >> I would like to know if a class definition has a decorator, > > I'm not sure what this question means. > > Applying a decorator to a class definition produces a normal class. > > Classes don't “have” decorators; classes can be returned by a decorator > function, but AFAIK the resulting class doesn't “have” the decorator in > any sense. It seems to me that a class decorator is (usually) like a class factory, in that it returns a class; the difference being that it takes a pre- existing class as argument, and (probably) modifies it in place, rather than creates a new class from scratch. I say "usually" and "probably" because, of course, a class decorator can do *anything*. Even something pointless: >>> def decorator(cls): ... return 1 ... >>> @decorator ... class K: ... pass ... >>> K 1 >> is that possible ? > > The return value of a decorator isn't special in any way, AFAIK. > > Any function can return a class object or a function object, and any > function can be used as a decorator. [pedant] Any callable can be a decorator, provided it has an appropriate calling signature. But you knew that :) [/pedant] > The only thing that makes a function a decorator is how it is used in > the code; but it doesn't leave a trace that I know of. Function decorators can, because they usually wrap the input function in a closure, which is detectable: >>> def decorator(func): ... def inner(): ... return func("spam") ... return inner ... >>> >>> @decorator ... def ham(s): ... return s.upper() ... >>> ham.__closure__ (,) >>> >>> def cheese(s): # no decorator ... return s.upper() ... >>> cheese.__closure__ >>> But this is only a common practice, not a guarantee, because the decorating function can do anything. I think that the only way to find out what was used to decorate a class, or a function, is for the decorator itself to leave some sort of mark on the wrapped class/function. Perhaps by adding itself to the wrapped object as an attribute: def decorate(cls): cls._decorated_by = decorate -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: is it possible to see if a class has a decorator ?
On 06-12-2010 12:08, Ben Finney wrote: > Stef Mientki writes: > >> I would like to know if a class definition has a decorator, > I'm not sure what this question means. > > Applying a decorator to a class definition produces a normal class. > > Classes don't “have” decorators; classes can be returned by a decorator > function, but AFAIK the resulting class doesn't “have” the decorator in > any sense. > >> is that possible ? > The return value of a decorator isn't special in any way, AFAIK. > > Any function can return a class object or a function object, and any > function can be used as a decorator. > > The only thing that makes a function a decorator is how it is used in > the code; but it doesn't leave a trace that I know of. > > Now, what is it you're trying to do? Perhaps there's a better solution > we can come up with. > Thanks Ben, here some more explanation. I've a number of (dynamic) applications, launched from a central wrapper. All these modules have a class "Start", which launches the application and embeds them in the wrapper application. Module 1: class Start (): Module 2: @auth class Start (): ... When the wrapper application is started, it looks for all dynamic modules (without importing them), and list these application in a hierarchical tree. In the above axmple, I would like to know that the class "Start" in Module 2 has the decorator "Auth", *without importing the module*, (so depending on the user logged in, I can decide to add or not add the module to the hierarchical tree). thanks, Stef Mientki -- http://mail.python.org/mailman/listinfo/python-list
Re: is it possible to see if a class has a decorator ?
Stef Mientki writes: > I would like to know if a class definition has a decorator, I'm not sure what this question means. Applying a decorator to a class definition produces a normal class. Classes don't “have” decorators; classes can be returned by a decorator function, but AFAIK the resulting class doesn't “have” the decorator in any sense. > is that possible ? The return value of a decorator isn't special in any way, AFAIK. Any function can return a class object or a function object, and any function can be used as a decorator. The only thing that makes a function a decorator is how it is used in the code; but it doesn't leave a trace that I know of. Now, what is it you're trying to do? Perhaps there's a better solution we can come up with. -- \ “Pinky, are you pondering what I'm pondering?” “I think so, | `\ Brain, but there's still a bug stuck in here from last time.” | _o__) —_Pinky and The Brain_ | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list
is it possible to see if a class has a decorator ?
hello, I would like to know if a class definition has a decorator, is that possible ? And if so, is it possible to determine the name of these decorator(s) ? thanks, Stef Mientki -- http://mail.python.org/mailman/listinfo/python-list