Đóng gói là một trong những khái niệm cơ bản trong lập trình hướng đối tượng [OOP] trong Python. Nó mô tả ý tưởng gói dữ liệu và các phương thức hoạt động trên dữ liệu trong một đơn vị. Điều này đặt ra các hạn chế đối với việc truy cập trực tiếp các biến và phương thức và có thể ngăn việc sửa đổi dữ liệu ngẫu nhiên. Một lớp là một ví dụ về đóng gói vì nó đóng gói tất cả dữ liệu là các hàm thành viên, biến, v.v. Bây giờ, có thể có một số tình huống trong đó chúng ta cần đặt các hạn chế đối với một số phương thức của lớp để chúng không thể được truy cập bên ngoài lớp cũng như bởi bất kỳ lớp con nào. Để thực hiện phương pháp riêng này đi vào chơi
P hàm riêng trong Python
Hãy xem xét một ví dụ thực tế, một động cơ ô tô, được tạo thành từ nhiều bộ phận như bugi, van, pít-tông, v.v. Không người dùng nào trực tiếp sử dụng các bộ phận này, thay vào đó họ chỉ biết cách sử dụng các bộ phận sử dụng chúng. Đây là những phương pháp riêng tư được sử dụng cho. Nó được sử dụng để ẩn chức năng bên trong của bất kỳ lớp nào với thế giới bên ngoài. Các phương thức riêng tư là những phương thức không được truy cập bên ngoài lớp cũng như bởi bất kỳ lớp cơ sở nào. Trong Python, không tồn tại các phương thức Riêng tư không thể truy cập ngoại trừ bên trong một lớp. Tuy nhiên, để xác định một phương thức riêng, tiền tố tên thành viên có dấu gạch dưới kép “__”. Ghi chú. Phương thức __init__ là một hàm tạo và chạy ngay khi một đối tượng của một lớp được khởi tạo.
Python3
# Creating a Base class
class
Base:
# Declaring public method
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'0
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'1
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'2
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'3
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'4_______0_______5_______0_______6
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'7_______0_______8
Public method Private method0
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'0
Public method Private method3
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'2
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'3
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'4_______0_______5_______0_______6
Public method Private method9
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'8
Private method1
class
Private method3
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'0
Private method6
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'2
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'3
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'4_______85_______0
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'4
# Creating a Base class
2Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'4_______85_______4
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'2
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'8
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'0
# Creating a Base class
9Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'2
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'3
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'4
class
3Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'4_______0_______5_______0_______6
class
7_______0_______8Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'4_______0_______2
Base:
1
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'0
Base:
4Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'2
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'3
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'4_______87_______8
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'4_______0_______2
1
2
3____88_______4
5
6
7
8_______88_______4 # Declaring public method
0
# Declaring public method
1
# Declaring public method
2
# Declaring public method
3
# Declaring public method
4
# Declaring public method
5
đầu ra
Public method Inside derived class Public method
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'
Ví dụ trên cho thấy rằng các phương thức private của lớp không thể được truy cập bên ngoài lớp cũng như bởi bất kỳ lớp cơ sở nào. Tuy nhiên, các phương thức riêng tư có thể được truy cập bằng cách gọi các phương thức riêng tư thông qua các phương thức công khai.
Thí dụ.
Python3
# Declaring public method
6
class
# Declaring public method
8
# Declaring public method
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'0
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'1
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'2
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'3
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'4_______0_______5_______0_______6
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'7_______0_______8
Public method Private method0
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'0
Public method Private method3
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'2
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'3
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'4_______0_______5_______0_______6
Public method Private method9
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'8
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'14
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'16
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'0
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'19
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'6
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'2
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'3
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'4_______0_______2
Base:
1Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'4_______0_______2
1
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'29
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'30_______88_______4
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'32
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'33
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'19
Traceback [most recent call last]: File "/home/09d6f91fdb63d16200e172c7a925dd7f.py", line 43, in obj1.__fun[] AttributeError: 'Base' object has no attribute '__fun' Traceback [most recent call last]: File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 46, in obj2.call_private[] File "/home/0d5506bab8f06cb7c842501d9704557b.py", line 32, in call_private self.__fun[] AttributeError: 'Derived' object has no attribute '_Derived__fun'35
đầu ra
Public method Private method
xáo trộn tên
Python cung cấp một cây đũa thần có thể được sử dụng để gọi các phương thức riêng tư bên ngoài lớp, nó được gọi là xáo trộn tên. Điều đó có nghĩa là bất kỳ mã định danh nào có dạng __geek [ít nhất hai dấu gạch dưới ở đầu hoặc nhiều nhất là một dấu gạch dưới ở cuối] được thay thế bằng _classname__geek, trong đó tên lớp là tên lớp hiện tại với [các] dấu gạch dưới ở đầu bị tước.