This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Cannot use Decorators of the same class that requires an instance of itself to change variables in that class.
Type: Stage: resolved
Components: Versions: Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Decorater, r.david.murray, steven.daprano
Priority: normal Keywords:

Created on 2017-03-18 15:51 by Decorater, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg289818 - (view) Author: Decorater (Decorater) * Date: 2017-03-18 15:51
So, many people rely on Decorators in class that accepts an instance of itself like so:

class ExampleClass:
    """Example class with an example decorator that cannot be used."""

    def __init__(self):
        self.list_of_items = []

    def add_item(self, item):
        self.list_of_items.append(item)

    @self.add_item("test_item")
    def test_item():
        print("Example function of ExampleClass that demonstrates the inability to use decorators with self passed to it.")

Many people fall for this on classes and then they are like "Why is it not letting me do this?". As such there is got to be a way to somehow support something like this in Python 3.7 as it could be useful on classes like this. The class above is an example, however I know of an library out there that allows you to import from a file and also allows you to use the same thing (that is imported) that would be bound to "self.[whatever it is called in the class]". As such people try to avoid that import and use the one in "self.[whatever it is called in the class]" to try to fit their needs (which ends up failing for them).
msg289819 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2017-03-18 16:06
Its not clear what you are asking for here. Do you think the current behaviour is a bug? Are you asking for a new feature? What do you want?

When the decorator is called, "self" doesn't exist, so of course @self.decorator *must* fail. What else could it do? Which instance should Python use, if there is no instance that exists yet?

You say "many people rely on Decorators in class that accepts an instance of itself", but I doubt that. I've never wanted code like that, and the example you show wouldn't work even if "self" existed. Can you give a better example?

Personally, I think this is a good learning experience for programmers, not something that needs to be fixed. Anyone who tries to decorate a method in a class by calling self.method is confused about classes, instances and decorators, and this is a good lesson for them to learn.
msg289820 - (view) Author: Decorater (Decorater) * Date: 2017-03-18 16:21
hmm, I see. Well I was looking for an way to actually be able to use decorators made in the same class to be able to somehow pass in the same instance as well. Currently the only way to use class decorators is for them to be static (which would not allow using or changing variables to the class (or functions in other classes bound to self). So, yeah there is got to be a way to do this, to use an decorator that takes in the same instance of a class.

On the other hand they get realize there is a certain way to use classes, decorators, and instances.
msg289828 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-03-18 20:40
Decorators are called with the decorated *function* objection when the class is compiled.  There can be no instance involved by their very nature, since the instance doesn't exist yet.  So no, you can't have a decorator that affects instance attributes at the compile step.  You *can* have a decorator that *effectively* manipulates instance attributes by returning a wrapper function, which will receive self when called, and can do whatever it wants.

There is no bug here, nor any need for a feature.  You can already do what you want, you just have to write your decorator correctly.  You can even have it as a static method of the class, though I'm not sure I'd consider that good style (but people's opinions on style differ).
History
Date User Action Args
2022-04-11 14:58:44adminsetgithub: 74034
2017-03-18 20:40:41r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg289828

resolution: not a bug
stage: resolved
2017-03-18 16:21:15Decoratersetmessages: + msg289820
2017-03-18 16:06:14steven.dapranosetnosy: + steven.daprano
messages: + msg289819
2017-03-18 15:51:36Decoratercreate