[issue29848] Cannot use Decorators of the same class that requires an instance of itself to change variables in that class.

2017-03-18 Thread R. David Murray

R. David Murray added the comment:

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).

--
nosy: +r.david.murray
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue29848] Cannot use Decorators of the same class that requires an instance of itself to change variables in that class.

2017-03-18 Thread Decorater

Decorater added the comment:

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.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue29848] Cannot use Decorators of the same class that requires an instance of itself to change variables in that class.

2017-03-18 Thread Steven D'Aprano

Steven D'Aprano added the comment:

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.

--
nosy: +steven.daprano

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue29848] Cannot use Decorators of the same class that requires an instance of itself to change variables in that class.

2017-03-18 Thread Decorater

New submission from Decorater:

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).

--
messages: 289818
nosy: Decorater
priority: normal
severity: normal
status: open
title: Cannot use Decorators of the same class that requires an instance of 
itself to change variables in that class.
versions: Python 3.7

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com