Hướng dẫn python access parent instance variable - python truy cập biến đối tượng gốc

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

Hướng dẫn python access parent instance variable - python truy cập biến đối tượng gốc

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
COE
7
COE
COE
SHIVAM
SACHIN
3425
3624
COE
8

COE
COE
SHIVAM
SACHIN
3425
3624
COE
7
Rahul
886012
30000000
0

COE
COE
SHIVAM
SACHIN
3425
3624
COE
7
Rahul
886012
30000000
2

COE
COE
SHIVAM
SACHIN
3425
3624
COE
7
Rahul
886012
30000000
4

COE
COE
SHIVAM
SACHIN
3425
3624
COE
7
Rahul
886012
30000000
6

COE
COE
SHIVAM
SACHIN
3425
3624
COE
7
Rahul
886012
30000000
8

COE
COE
SHIVAM
SACHIN
3425
3624
COE
7
SINGLA ELECTRONICS
DELL Inspiron 15
Redmi Note 5
0

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 5
2
SINGLA ELECTRONICS
DELL Inspiron 15
Redmi Note 5
3
SINGLA ELECTRONICS
DELL Inspiron 15
Redmi Note 5
4

print self._haircolor
3
AttributeError: type object 'Mother' has no attribute '_haircolor'
9
print self._haircolor
0
print self._haircolor
1
SINGLA ELECTRONICS
DELL Inspiron 15
Redmi Note 5
9

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
1
print self._haircolor
5
AttributeError: type object 'Mother' has no attribute '_haircolor'
6
print self._haircolor
7

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
1
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!
77__16

print self._haircolor
3
AttributeError: type object 'Mother' has no attribute '_haircolor'
9 self._haircolor2
print self._haircolor
1self._haircolor4

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
COE
COE
SHIVAM
SACHIN
3425
3624
COE
7self._haircolor7
print self._haircolor
1self._haircolor9

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
COE
COE
SHIVAM
SACHIN
3425
3624
COE
7self._haircolor7
print self._haircolor
1super4

AttributeError: type object 'Mother' has no attribute '_haircolor'
2 super6

print self._haircolor
3
AttributeError: type object 'Mother' has no attribute '_haircolor'
9
print self._haircolor
0
print self._haircolor
1
AttributeError: type object 'Mother' has no attribute '_haircolor'
01

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
1
AttributeError: type object 'Mother' has no attribute '_haircolor'
04
AttributeError: type object 'Mother' has no attribute '_haircolor'
6
AttributeError: type object 'Mother' has no attribute '_haircolor'
06

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
AttributeError: type object 'Mother' has no attribute '_haircolor'
08
print self._haircolor
1
AttributeError: type object 'Mother' has no attribute '_haircolor'
10

print self._haircolor
3
AttributeError: type object 'Mother' has no attribute '_haircolor'
9
AttributeError: type object 'Mother' has no attribute '_haircolor'
13
print self._haircolor
1
AttributeError: type object 'Mother' has no attribute '_haircolor'
15

AttributeError: type object 'Mother' has no attribute '_haircolor'
16
COE
COE
SHIVAM
SACHIN
3425
3624
COE
7self._haircolor7
print self._haircolor
1
AttributeError: type object 'Mother' has no attribute '_haircolor'
20

class Child(Mother):
    def __init__(self): 
        super(Child, self).__init__()
    def print_haircolor(self):
        print self._haircolor
3
AttributeError: type object 'Mother' has no attribute '_haircolor'
6
AttributeError: type object 'Mother' has no attribute '_haircolor'
23
AttributeError: type object 'Mother' has no attribute '_haircolor'
24
class Child(Mother):
    def __init__(self): 
        super(Child, self).__init__()
    def print_haircolor(self):
        print self._haircolor
7
AttributeError: type object 'Mother' has no attribute '_haircolor'
26
class Child(Mother):
    def __init__(self): 
        super(Child, self).__init__()
    def print_haircolor(self):
        print self._haircolor
7
AttributeError: type object 'Mother' has no attribute '_haircolor'
28
AttributeError: type object 'Mother' has no attribute '_haircolor'
29

AttributeError: type object 'Mother' has no attribute '_haircolor'
30

AttributeError: type object 'Mother' has no attribute '_haircolor'
31

Output:

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.

Hướng dẫn python access parent instance variable - python truy cập biến đối tượng gốc

Example:

AttributeError: type object 'Mother' has no attribute '_haircolor'
2
AttributeError: type object 'Mother' has no attribute '_haircolor'
33

AttributeError: type object 'Mother' has no attribute '_haircolor'
4
AttributeError: type object 'Mother' has no attribute '_haircolor'
9
print self._haircolor
0
print self._haircolor
1
AttributeError: type object 'Mother' has no attribute '_haircolor'
15

print self._haircolor
3
COE
COE
SHIVAM
SACHIN
3425
3624
COE
7self._haircolor7
AttributeError: type object 'Mother' has no attribute '_haircolor'
42
COE
COE
SHIVAM
SACHIN
3425
3624
COE
6

print self._haircolor
3
print self._haircolor
1
AttributeError: type object 'Mother' has no attribute '_haircolor'
46
AttributeError: type object 'Mother' has no attribute '_haircolor'
6
print self._haircolor
1
AttributeError: type object 'Mother' has no attribute '_haircolor'
49

print self._haircolor
3
print self._haircolor
1
AttributeError: type object 'Mother' has no attribute '_haircolor'
52
AttributeError: type object 'Mother' has no attribute '_haircolor'
6
print self._haircolor
1
AttributeError: type object 'Mother' has no attribute '_haircolor'
55

AttributeError: type object 'Mother' has no attribute '_haircolor'
4
AttributeError: type object 'Mother' has no attribute '_haircolor'
2
AttributeError: type object 'Mother' has no attribute '_haircolor'
58

print self._haircolor
3
AttributeError: type object 'Mother' has no attribute '_haircolor'
9
AttributeError: type object 'Mother' has no attribute '_haircolor'
61
print self._haircolor
1
AttributeError: type object 'Mother' has no attribute '_haircolor'
15

AttributeError: type object 'Mother' has no attribute '_haircolor'
16
COE
COE
SHIVAM
SACHIN
3425
3624
COE
7self._haircolor7
AttributeError: type object 'Mother' has no attribute '_haircolor'
67
COE
COE
SHIVAM
SACHIN
3425
3624
COE
6

AttributeError: type object 'Mother' has no attribute '_haircolor'
4
AttributeError: type object 'Mother' has no attribute '_haircolor'
2
AttributeError: type object 'Mother' has no attribute '_haircolor'
71

print self._haircolor
3
AttributeError: type object 'Mother' has no attribute '_haircolor'
9
AttributeError: type object 'Mother' has no attribute '_haircolor'
61
print self._haircolor
1
AttributeError: type object 'Mother' has no attribute '_haircolor'
15

AttributeError: type object 'Mother' has no attribute '_haircolor'
16
COE
COE
SHIVAM
SACHIN
3425
3624
COE
7self._haircolor7
AttributeError: type object 'Mother' has no attribute '_haircolor'
80
COE
COE
SHIVAM
SACHIN
3425
3624
COE
6

AttributeError: type object 'Mother' has no attribute '_haircolor'
4
AttributeError: type object 'Mother' has no attribute '_haircolor'
2
AttributeError: type object 'Mother' has no attribute '_haircolor'
71

AttributeError: type object 'Mother' has no attribute '_haircolor'
85

AttributeError: type object 'Mother' has no attribute '_haircolor'
86

Output:

SINGLA ELECTRONICS
DELL Inspiron 15
Redmi Note 5

AttributeError: type object 'Mother' has no attribute '_haircolor'
82
AttributeError: 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.

Hướng dẫn python access parent instance variable - python truy cập biến đối tượng gốc

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'
4
AttributeError: type object 'Mother' has no attribute '_haircolor'
9
print self._haircolor
0
print self._haircolor
1
AttributeError: type object 'Mother' has no attribute '_haircolor'
15

print self._haircolor
3
COE
COE
SHIVAM
SACHIN
3425
3624
COE
7self._haircolor7
AttributeError: type object 'Mother' has no attribute '_haircolor'
42
COE
COE
SHIVAM
SACHIN
3425
3624
COE
6

AttributeError: type object 'Mother' has no attribute '_haircolor'
4
AttributeError: type object 'Mother' has no attribute '_haircolor'
2
AttributeError: type object 'Mother' has no attribute '_haircolor'
58

print self._haircolor
3
AttributeError: type object 'Mother' has no attribute '_haircolor'
9
AttributeError: type object 'Mother' has no attribute '_haircolor'
61
print self._haircolor
1
AttributeError: type object 'Mother' has no attribute '_haircolor'
15

print self._haircolor
3
COE
COE
SHIVAM
SACHIN
3425
3624
COE
7self._haircolor7
print self._haircolor
14
COE
COE
SHIVAM
SACHIN
3425
3624
COE
6

AttributeError: type object 'Mother' has no attribute '_haircolor'
4
AttributeError: type object 'Mother' has no attribute '_haircolor'
2
AttributeError: type object 'Mother' has no attribute '_haircolor'
71

AttributeError: type object 'Mother' has no attribute '_haircolor'
82
AttributeError: type object 'Mother' has no attribute '_haircolor'
6
AttributeError: type object 'Mother' has no attribute '_haircolor'
84

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'
2
AttributeError: type object 'Mother' has no attribute '_haircolor'
88

AttributeError: type object 'Mother' has no attribute '_haircolor'
16
COE
COE
SHIVAM
SACHIN
3425
3624
COE
7self._haircolor7
print self._haircolor
38
COE
COE
SHIVAM
SACHIN
3425
3624
COE
6

print self._haircolor
3
print self._haircolor
1
AttributeError: type object 'Mother' has no attribute '_haircolor'
96
AttributeError: type object 'Mother' has no attribute '_haircolor'
6
print self._haircolor
1
AttributeError: type object 'Mother' has no attribute '_haircolor'
99

print self._haircolor
3
print self._haircolor
1
print self._haircolor
0216
print self._haircolor
1
print self._haircolor
05

AttributeError: type object 'Mother' has no attribute '_haircolor'
4
AttributeError: type object 'Mother' has no attribute '_haircolor'
9
print self._haircolor
08
print self._haircolor
1
AttributeError: type object 'Mother' has no attribute '_haircolor'
15

AttributeError: type object 'Mother' has no attribute '_haircolor'
4
AttributeError: type object 'Mother' has no attribute '_haircolor'
2
print self._haircolor
18

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
COE
COE
SHIVAM
SACHIN
3425
3624
COE
7
print self._haircolor
61

print self._haircolor
3
AttributeError: type object 'Mother' has no attribute '_haircolor'
9
print self._haircolor
0
print self._haircolor
1
AttributeError: type object 'Mother' has no attribute '_haircolor'
15

print self._haircolor
65

AttributeError: type object 'Mother' has no attribute '_haircolor'
16
print self._haircolor
1
print self._haircolor
0216
print self._haircolor
1
print self._haircolor
29

print self._haircolor
69

print self._haircolor
3
AttributeError: type object 'Mother' has no attribute '_haircolor'
9
print self._haircolor
08
print self._haircolor
1
AttributeError: type object 'Mother' has no attribute '_haircolor'
15

print self._haircolor
73
print self._haircolor
74
COE
COE
SHIVAM
SACHIN
3425
3624
COE
6

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!

Các biến thể hiện của lớp con có thể truy cập các biến của lớp cha không?

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.you can't directly access parent class instance variables. For example, if the parent had a height instance variable, child class method definitions wouldn't be able to access this directly.

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.The function returns a temporary object that allows reference to a parent class by the keyword super. The super() function has two major use cases: To avoid the usage of the super (parent) class explicitly.

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.there is no existence of “Private” instance variables that cannot be accessed except inside an object.

__ 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.can be used to obtain a list of classes the given class inherits. The __bases__ property of the class contains a list of all the base classes that the given class inherits.