On Tue, Jun 1, 2021 at 10:16 PM Steven D'Aprano <st...@pearwood.info> wrote:
> The only slightly awkward case is the bare variable case. Most of the
> time there will be no overlap between the function/class decorators and
> the bare variable decorator, but in the rare case that we need to use a
> single function in both cases, we can easily distinguish the two cases:
>
>     def mydecorator(arg, **kwargs):
>         if isinstance(arg, str):
>             # must be decorating a variable
>             ...
>         else:
>             # decorating a function or class
>             assert kwarg == {}
>
> So it is easy to handle both uses in a single function, but I emphasise
> that this would be rare. Normally a single decorator would be used in
> the function/class case, or the variable case, but not both.
>

I can't imagine any situation where you would *want* this, but it is
actually possible for a decorator to be given a string:

>>> def mydecorator(arg, **kwargs):
...     if isinstance(arg, str):
...             print("Decorating a variable can't happen, right?")
...     else:
...             print("Good, we're decorating a function.")
...     return arg
...
>>> @mydecorator
... @str
... def f(): ...
...
Decorating a variable can't happen, right?
>>>

This probably falls under "you shot yourself in the foot, so now you
have a foot with a hole in it".

ChrisA
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/YAQENPUEEOQGPUUHG6ST3L4IEXOQM4X4/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to