is it possible to see if a class has a decorator ?

S

Stef Mientki

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
 
S

Steven D'Aprano

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:

.... return 1
........ class K:
.... pass
....
1




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 inner():
.... return func("spam")
.... return inner
........ def ham(s):
.... return s.upper()
........ return s.upper()
....
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
 
J

Jean-Michel Pichavant

Stef said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,182
Latest member
BettinaPol

Latest Threads

Top