Tôi đang cố gắng truy cập một biến thành viên phụ huynh từ một lớp mở rộng. Nhưng chạy mã sau ...
class Mother[object]:
def __init__[self]:
self._haircolor = "Brown"
class Child[Mother]:
def __init__[self]:
Mother.__init__[self]
def print_haircolor[self]:
print Mother._haircolor
c = Child[]
c.print_haircolor[]
Nhận được lỗi này:
AttributeError: type object 'Mother' has no attribute '_haircolor'
Tôi đang làm gì sai?
Hỏi ngày 8 tháng 4 năm 2012 lúc 17:12Apr 8, 2012 at 17:12
Bạn đang trộn lẫn các thuộc tính lớp và thể hiện.
print self._haircolor
Đã trả lời ngày 8 tháng 4 năm 2012 lúc 17:15Apr 8, 2012 at 17:15
0
Bạn muốn thuộc tính thể hiện, không phải thuộc tính lớp, vì vậy bạn nên sử dụng self._haircolor
.
Ngoài ra, bạn thực sự nên sử dụng super
trong
AttributeError: type object 'Mother' has no attribute '_haircolor'
0 trong trường hợp bạn quyết định thay đổi quyền thừa kế của mình thành AttributeError: type object 'Mother' has no attribute '_haircolor'
1 hoặc một cái gì đó.class Child[Mother]:
def __init__[self]:
super[Child, self].__init__[]
def print_haircolor[self]:
print self._haircolor
Đã trả lời ngày 8 tháng 4 năm 2012 lúc 17:17Apr 8, 2012 at 17:17
MvchrmvchrmVChr
48.8K11 Huy hiệu vàng106 Huy hiệu bạc101 Huy hiệu đồng11 gold badges106 silver badges101 bronze badges
2
Khía cạnh bất thường duy nhất là, trong các định nghĩa phương thức lớp con, bạn không thể truy cập trực tiếp các biến thể hiện của lớp phụ huynh. Ví dụ: nếu cha mẹ có biến thể hiện chiều cao, các định nghĩa phương thức lớp con sẽ không thể truy cập trực tiếp điều này.
Example:
Super [] hoạt động như thế nào trong Python?
Hàm Super [] trong Python làm cho kế thừa lớp trở nên dễ quản lý và có thể mở rộng hơn. Hàm trả về một đối tượng tạm thời cho phép tham chiếu đến lớp cha bằng từ khóa Super. Hàm Super [] có hai trường hợp sử dụng chính: để tránh việc sử dụng lớp siêu [cha mẹ] một cách rõ ràng.
Lớp trẻ có thể truy cập python biến riêng không?
Trong Python, không có sự tồn tại của các biến thể riêng tư của người Hồi giáo không thể truy cập ngoại trừ bên trong một đối tượng.
__ cơ sở __ trong Python là gì?
Python cung cấp thuộc tính __base__ trên mỗi lớp có thể được sử dụng để có được danh sách các lớp mà lớp đã cho. Thuộc tính __base__ của lớp chứa một danh sách tất cả các lớp cơ sở mà lớp đã cho kế thừa.
Một lớp là bản thiết kế hoặc nguyên mẫu do người dùng xác định từ đó các đối tượng được tạo. Các lớp cung cấp một phương tiện của dữ liệu bó và chức năng cùng nhau. Tạo một lớp mới tạo ra một loại đối tượng mới, cho phép các phiên bản mới của loại đó được thực hiện. Mỗi phiên bản lớp có thể có các thuộc tính được gắn vào nó để duy trì trạng thái của nó. Các phiên bản lớp cũng có thể có các phương thức [được xác định bởi lớp của nó] để sửa đổi trạng thái của nó.
COE COE SHIVAM SACHIN 3425 3624 COE7
COE COE SHIVAM SACHIN 3425 3624 COE8
COE COE SHIVAM SACHIN 3425 3624 COE7
Rahul 886012 300000000
COE COE SHIVAM SACHIN 3425 3624 COE7
Rahul 886012 300000002
COE COE SHIVAM SACHIN 3425 3624 COE7
Rahul 886012 300000004
COE COE SHIVAM SACHIN 3425 3624 COE7
Rahul 886012 300000006
COE COE SHIVAM SACHIN 3425 3624 COE7
Rahul 886012 300000008
COE COE SHIVAM SACHIN 3425 3624 COE7
SINGLA ELECTRONICS DELL Inspiron 15 Redmi Note 50
Output:
COE COE SHIVAM SACHIN 3425 3624 COE
Lưu ý: Để biết thêm thông tin, hãy tham khảo các lớp và đối tượng Python. For more information, refer to Python Classes and Objects.
Truy cập các chức năng của lớp cha
Khi một lớp kế thừa từ một lớp khác, nó kế thừa các thuộc tính và phương thức của một lớp khác. Một lớp kế thừa từ một lớp khác được gọi là lớp con và lớp mà lớp con được thừa hưởng được gọi là lớp cha. Nhưng bạn đã bao giờ tự hỏi làm thế nào để truy cập các phương pháp lớp cha mẹ? Điều này thực sự đơn giản, bạn chỉ cần gọi hàm tạo của lớp cha trong hàm tạo của lớp con và sau đó đối tượng của lớp con có thể truy cập các phương thức và thuộc tính của lớp cha.
Example:
AttributeError: type object 'Mother' has no attribute '_haircolor'
2 SINGLA ELECTRONICS DELL Inspiron 15 Redmi Note 52
SINGLA ELECTRONICS DELL Inspiron 15 Redmi Note 53
SINGLA ELECTRONICS DELL Inspiron 15 Redmi Note 54
print self._haircolor
3AttributeError: type object 'Mother' has no attribute '_haircolor'
9 print self._haircolor
0print self._haircolor
1SINGLA ELECTRONICS DELL Inspiron 15 Redmi Note 59
This is in Outer class that is Vehicle This is in Inner class that is Car This is in multilevel InnerInner class that is Maruti Just Print It!0
print self._haircolor
1print self._haircolor
5AttributeError: type object 'Mother' has no attribute '_haircolor'
6 print self._haircolor
7This is in Outer class that is Vehicle This is in Inner class that is Car This is in multilevel InnerInner class that is Maruti Just Print It!0
print self._haircolor
1This is in Outer class that is Vehicle This is in Inner class that is Car This is in multilevel InnerInner class that is Maruti Just Print It!77__16
print self._haircolor
3AttributeError: type object 'Mother' has no attribute '_haircolor'
9 self._haircolor
2print self._haircolor
1self._haircolor
4This is in Outer class that is Vehicle This is in Inner class that is Car This is in multilevel InnerInner class that is Maruti Just Print It!0
COE COE SHIVAM SACHIN 3425 3624 COE7
self._haircolor
7print self._haircolor
1self._haircolor
9This is in Outer class that is Vehicle This is in Inner class that is Car This is in multilevel InnerInner class that is Maruti Just Print It!0
COE COE SHIVAM SACHIN 3425 3624 COE7
self._haircolor
7print self._haircolor
1super
4AttributeError: type object 'Mother' has no attribute '_haircolor'
2 super
6print self._haircolor
3AttributeError: type object 'Mother' has no attribute '_haircolor'
9 print self._haircolor
0print self._haircolor
1AttributeError: type object 'Mother' has no attribute '_haircolor'
01This is in Outer class that is Vehicle This is in Inner class that is Car This is in multilevel InnerInner class that is Maruti Just Print It!0
print self._haircolor
1AttributeError: type object 'Mother' has no attribute '_haircolor'
04AttributeError: type object 'Mother' has no attribute '_haircolor'
6 AttributeError: type object 'Mother' has no attribute '_haircolor'
06This is in Outer class that is Vehicle This is in Inner class that is Car This is in multilevel InnerInner class that is Maruti Just Print It!0
AttributeError: type object 'Mother' has no attribute '_haircolor'
08print self._haircolor
1AttributeError: type object 'Mother' has no attribute '_haircolor'
10print self._haircolor
3AttributeError: type object 'Mother' has no attribute '_haircolor'
9 AttributeError: type object 'Mother' has no attribute '_haircolor'
13print self._haircolor
1AttributeError: type object 'Mother' has no attribute '_haircolor'
15AttributeError: type object 'Mother' has no attribute '_haircolor'
16COE COE SHIVAM SACHIN 3425 3624 COE7
self._haircolor
7print self._haircolor
1AttributeError: type object 'Mother' has no attribute '_haircolor'
20class Child[Mother]:
def __init__[self]:
super[Child, self].__init__[]
def print_haircolor[self]:
print self._haircolor
3AttributeError: type object 'Mother' has no attribute '_haircolor'
6 AttributeError: type object 'Mother' has no attribute '_haircolor'
23AttributeError: type object 'Mother' has no attribute '_haircolor'
24class Child[Mother]:
def __init__[self]:
super[Child, self].__init__[]
def print_haircolor[self]:
print self._haircolor
7AttributeError: type object 'Mother' has no attribute '_haircolor'
26class Child[Mother]:
def __init__[self]:
super[Child, self].__init__[]
def print_haircolor[self]:
print self._haircolor
7AttributeError: type object 'Mother' has no attribute '_haircolor'
28AttributeError: type object 'Mother' has no attribute '_haircolor'
29AttributeError: type object 'Mother' has no attribute '_haircolor'
30AttributeError: type object 'Mother' has no attribute '_haircolor'
31Output:
Rahul 886012 30000000
Lưu ý: Để biết thêm thông tin, hãy tham khảo kế thừa trong Python. For more information, refer to Inheritance in Python.
Truy cập phương thức lớp cha từ lớp bên trong
Một lớp bên trong hoặc lớp lồng nhau là một lớp được xác định bên trong cơ thể của một lớp khác. Nếu một đối tượng được tạo bằng một lớp, đối tượng bên trong lớp gốc có thể được sử dụng. Một lớp có thể có một hoặc nhiều hơn một lớp bên trong.
Các loại lớp bên trong:
- Nhiều lớp bên trong
- Lớp nội địa đa cấp
Nhiều lớp bên trong: Một lớp chứa nhiều hơn một lớp bên trong. A class containing more than one inner class.
Example:
AttributeError: type object 'Mother' has no attribute '_haircolor'
2 AttributeError: type object 'Mother' has no attribute '_haircolor'
33AttributeError: type object 'Mother' has no attribute '_haircolor'
4AttributeError: type object 'Mother' has no attribute '_haircolor'
9 print self._haircolor
0print self._haircolor
1AttributeError: type object 'Mother' has no attribute '_haircolor'
15print self._haircolor
3COE COE SHIVAM SACHIN 3425 3624 COE7
self._haircolor
7AttributeError: type object 'Mother' has no attribute '_haircolor'
42COE COE SHIVAM SACHIN 3425 3624 COE6
print self._haircolor
3print self._haircolor
1AttributeError: type object 'Mother' has no attribute '_haircolor'
46AttributeError: type object 'Mother' has no attribute '_haircolor'
6print self._haircolor
1AttributeError: type object 'Mother' has no attribute '_haircolor'
49print self._haircolor
3print self._haircolor
1AttributeError: type object 'Mother' has no attribute '_haircolor'
52AttributeError: type object 'Mother' has no attribute '_haircolor'
6print self._haircolor
1AttributeError: type object 'Mother' has no attribute '_haircolor'
55AttributeError: type object 'Mother' has no attribute '_haircolor'
4AttributeError: type object 'Mother' has no attribute '_haircolor'
2 AttributeError: type object 'Mother' has no attribute '_haircolor'
58print self._haircolor
3AttributeError: type object 'Mother' has no attribute '_haircolor'
9 AttributeError: type object 'Mother' has no attribute '_haircolor'
61print self._haircolor
1AttributeError: type object 'Mother' has no attribute '_haircolor'
15AttributeError: type object 'Mother' has no attribute '_haircolor'
16COE COE SHIVAM SACHIN 3425 3624 COE7
self._haircolor
7AttributeError: type object 'Mother' has no attribute '_haircolor'
67COE COE SHIVAM SACHIN 3425 3624 COE6
AttributeError: type object 'Mother' has no attribute '_haircolor'
4AttributeError: type object 'Mother' has no attribute '_haircolor'
2 AttributeError: type object 'Mother' has no attribute '_haircolor'
71print self._haircolor
3AttributeError: type object 'Mother' has no attribute '_haircolor'
9 AttributeError: type object 'Mother' has no attribute '_haircolor'
61print self._haircolor
1AttributeError: type object 'Mother' has no attribute '_haircolor'
15AttributeError: type object 'Mother' has no attribute '_haircolor'
16COE COE SHIVAM SACHIN 3425 3624 COE7
self._haircolor
7AttributeError: type object 'Mother' has no attribute '_haircolor'
80COE COE SHIVAM SACHIN 3425 3624 COE6
AttributeError: type object 'Mother' has no attribute '_haircolor'
4AttributeError: type object 'Mother' has no attribute '_haircolor'
2 AttributeError: type object 'Mother' has no attribute '_haircolor'
71AttributeError: type object 'Mother' has no attribute '_haircolor'
85AttributeError: type object 'Mother' has no attribute '_haircolor'
86Output:
SINGLA ELECTRONICS DELL Inspiron 15 Redmi Note 5
82AttributeError: type object 'Mother' has no attribute '_haircolor'
6 AttributeError: type object 'Mother' has no attribute '_haircolor'
84 In multilevel inner classes, the inner class contains another class which is inner classes to the previous one.AttributeError: type object 'Mother' has no attribute '_haircolor'
Example:
Lớp bên trong đa cấp: Trong các lớp bên trong đa cấp, lớp bên trong chứa một lớp khác là các lớp bên trong của lớp trước.
AttributeError: type object 'Mother' has no attribute '_haircolor'
4AttributeError: type object 'Mother' has no attribute '_haircolor'
9 print self._haircolor
0print self._haircolor
1AttributeError: type object 'Mother' has no attribute '_haircolor'
15print self._haircolor
3COE COE SHIVAM SACHIN 3425 3624 COE7
self._haircolor
7AttributeError: type object 'Mother' has no attribute '_haircolor'
42COE COE SHIVAM SACHIN 3425 3624 COE6
AttributeError: type object 'Mother' has no attribute '_haircolor'
4AttributeError: type object 'Mother' has no attribute '_haircolor'
2 AttributeError: type object 'Mother' has no attribute '_haircolor'
58print self._haircolor
3AttributeError: type object 'Mother' has no attribute '_haircolor'
9 AttributeError: type object 'Mother' has no attribute '_haircolor'
61print self._haircolor
1AttributeError: type object 'Mother' has no attribute '_haircolor'
15print self._haircolor
3COE COE SHIVAM SACHIN 3425 3624 COE7
self._haircolor
7print self._haircolor
14COE COE SHIVAM SACHIN 3425 3624 COE6
AttributeError: type object 'Mother' has no attribute '_haircolor'
4AttributeError: type object 'Mother' has no attribute '_haircolor'
2 AttributeError: type object 'Mother' has no attribute '_haircolor'
71AttributeError: type object 'Mother' has no attribute '_haircolor'
82AttributeError: type object 'Mother' has no attribute '_haircolor'
6 AttributeError: type object 'Mother' has no attribute '_haircolor'
84Lớp bên trong đa cấp: Trong các lớp bên trong đa cấp, lớp bên trong chứa một lớp khác là các lớp bên trong của lớp trước.
AttributeError: type object 'Mother' has no attribute '_haircolor'
2 AttributeError: type object 'Mother' has no attribute '_haircolor'
88AttributeError: type object 'Mother' has no attribute '_haircolor'
16COE COE SHIVAM SACHIN 3425 3624 COE7
self._haircolor
7print self._haircolor
38COE COE SHIVAM SACHIN 3425 3624 COE6
print self._haircolor
3print self._haircolor
1AttributeError: type object 'Mother' has no attribute '_haircolor'
96AttributeError: type object 'Mother' has no attribute '_haircolor'
6 print self._haircolor
1AttributeError: type object 'Mother' has no attribute '_haircolor'
99print self._haircolor
3print self._haircolor
1print self._haircolor
0216 print self._haircolor
1print self._haircolor
05AttributeError: type object 'Mother' has no attribute '_haircolor'
4AttributeError: type object 'Mother' has no attribute '_haircolor'
9 print self._haircolor
08print self._haircolor
1AttributeError: type object 'Mother' has no attribute '_haircolor'
15AttributeError: type object 'Mother' has no attribute '_haircolor'
4AttributeError: type object 'Mother' has no attribute '_haircolor'
2 print self._haircolor
18This is in Outer class that is Vehicle This is in Inner class that is Car This is in multilevel InnerInner class that is Maruti Just Print It!0
COE COE SHIVAM SACHIN 3425 3624 COE7
print self._haircolor
61print self._haircolor
3AttributeError: type object 'Mother' has no attribute '_haircolor'
9 print self._haircolor
0print self._haircolor
1AttributeError: type object 'Mother' has no attribute '_haircolor'
15print self._haircolor
65AttributeError: type object 'Mother' has no attribute '_haircolor'
16print self._haircolor
1print self._haircolor
0216 print self._haircolor
1print self._haircolor
29print self._haircolor
69print self._haircolor
3AttributeError: type object 'Mother' has no attribute '_haircolor'
9 print self._haircolor
08print self._haircolor
1AttributeError: type object 'Mother' has no attribute '_haircolor'
15print self._haircolor
73print self._haircolor
74COE COE SHIVAM SACHIN 3425 3624 COE6
Output:
This is in Outer class that is Vehicle This is in Inner class that is Car This is in multilevel InnerInner class that is Maruti Just Print It!