-
-
Notifications
You must be signed in to change notification settings - Fork 29.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Assigning attribute to instance method causes AttributeError #100057
Comments
For backreference: sanic-org/sanic#2581 |
A method object wraps a callable, which is usually a function object. The real attributes of a method object include its For a wrapped function object, the latter includes A method object does not have its own |
Thanks @eryksun. I was not aware of |
Any class that provides dynamic attributes by overriding class C:
__slots__ = ()
def __getattr__(self, name):
return 'spam' >>> c = C()
>>> c.x
'spam'
>>> c.x = 'eggs'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'C' object has no attribute 'x' That said, I don't see a reason to not override the |
@vstinner, do you think it would be helpful to implement a custom |
There are many "read-only type", especially ones implemented in C, which cannot be modified, whereas types defined in Python can be modified. If you want to change that, I suggest to open a wider discussion on the python-ideas list or in the Core Development category in: https://discuss.python.org/ In Python 3.10, Py_TPFLAGS_IMMUTABLETYPE flag was added to make this feature a little bit more explicit and visible: In short, it's also a deliberate choice that some types cannot be modified. See: #88074 (comment) |
@vstinner, I wasn't suggesting to allow modification of a method object's real attributes (e.g. This issue is in regards to trying to modify the attributes of the underlying callable, which A |
Correct. The practical side of me wishes that it would pass thru so that it would "just work" in that a function or an instance method could be passed around and used (and potentially modified with dynamic attributes) without knowing what it is. But, I certainly understand that it is a bit of drawing lines in the sand: what is modifiable? what is not? Since the behavior is not intuitive the error: |
Bug report
Assigning (and reassigning) attributes to functions works as expected as with any object.
However, the experience is not consistent with class methods. When dealing with the class, you can assign and reassign similar to regular functions.
This even carries through after instantiation:
We can clearly see the value exists in the
__dict__
:However, trying to assign to the method from the instance fails:
While I guess I can see why this might be (since you would be modifying an object on the class and not the instance--if this indeed is the rationale), this is not intuitive. It seems inconsistent with setting attributes otherwise.
If this behavior is intended and not a bug, then perhaps the error should be more explicit. The error stating that the method has no attribute named
something
is not correct. It does have the attribute and we can otherwise interact with it, just not set it.Either we should be able to assign back to that attribute from the instance, or we should receive a more explicit error why this is not possible.
Your environment
The text was updated successfully, but these errors were encountered: