python / python/cpython

Assigning attribute to instance method causes AttributeError

Open
#100,057 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

interpreter-core type-bug
Dominant language
Python
Stars
77.2k
Forks
36k
PR merge metrics
PR metrics pending

Description

Bug report

Assigning (and reassigning) attributes to functions works as expected as with any object.

def bar(self):
    ...


bar.something = 1
print(bar.something)
# prints: 1

bar.something = 2
print(bar.something)
# prints: 2

However, the experience is not consistent with class methods. When dealing with the class, you can assign and reassign similar to regular functions.

class Foo:
    def bar(self):
        ...


Foo.bar.something = 1
print(Foo.bar.something)
# prints: 1

Foo.bar.something = 2
print(Foo.bar.something)
# prints: 2

This even carries through after instantiation:

foo = Foo()
print(foo.bar.something)
# prints: 2

We can clearly see the value exists in the __dict__:

print(foo.bar.__dict__)
# {'something': 2}

However, trying to assign to the method from the instance fails:

foo.bar.something = 2
# AttributeError: 'method' object has no attribute 'something'

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

  • CPython versions tested on: 3.7.12, 3.8.11, 3.9.9, 3.10.8, 3.11.0rc1
  • Operating system and architecture:
▶ uname -a           
Linux hopkins 6.0.2-arch1-1 #1 SMP PREEMPT_DYNAMIC Sat, 15 Oct 2022 14:00:49 +0000 x86_64 GNU/Linux

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the instance-method attribute assignment examples from the issue across the affected Python versions. Review the method attribute and descriptor behavior before deciding whether assignment should be supported or the error clarified; done requires an agreed behavior and corresponding regression coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.