python / python/cpython

Assigning attribute to instance method causes AttributeError

Đang mở
#100,057 8 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

interpreter-core type-bug
Ngôn ngữ chính
Python
Star
77.2k
Fork
36k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu bằng cách tái hiện các ví dụ về việc gán thuộc tính cho phương thức instance từ issue trên các phiên bản Python bị ảnh hưởng. Xem xét hành vi của thuộc tính phương thức và descriptor trước khi quyết định có nên hỗ trợ phép gán hay làm rõ lỗi; công việc được xem là hoàn tất khi đã thống nhất hành vi và có coverage hồi quy tương ứng.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
backend
Loại issue
Lỗi
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Cần làm rõ
Mức phù hợp với người mới
25/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.