Một bản nhấn mạnh ở đầu:
Python không có phương pháp riêng tư thực sự. Thay vào đó, một người nhấn mạnh khi bắt đầu một phương thức hoặc tên thuộc tính có nghĩa là bạn không nên truy cập phương thức này, vì nó không phải là một phần của API.
class BaseForm[StrAndUnicode]:
def _get_errors[self]:
"Returns an ErrorDict for the data provided for the form"
if self._errors is None:
self.full_clean[]
return self._errors
errors = property[_get_errors]
. Trong mã này, errors
là một tài sản công cộng, nhưng phương thức thuộc tính này gọi, _get_errors, là "riêng tư", vì vậy bạn không nên truy cập nó.
Hai dấu gạch dưới lúc đầu:
Điều này gây ra rất nhiều sự nhầm lẫn. Nó không nên được sử dụng để tạo ra một phương thức riêng tư. Nó nên được sử dụng để tránh phương pháp của bạn bị ghi đè bởi một lớp con hoặc vô tình truy cập. Hãy xem một ví dụ:
class A[object]:
def __test[self]:
print "I'm a test method in class A"
def test[self]:
self.__test[]
a = A[]
a.test[]
# a.__test[] # This fails with an AttributeError
a._A__test[] # Works! We can access the mangled name directly!
Đầu ra:
$ python test.py
I'm test method in class A
I'm test method in class A
Bây giờ tạo một lớp B và tùy chỉnh cho phương thức __test
class B[A]:
def __test[self]:
print "I'm test method in class B"
b = B[]
b.test[]
Đầu ra sẽ là ....
$ python test.py
I'm test method in class A
Như chúng ta đã thấy, a.test [] đã không gọi các phương thức b .__ test [], như chúng ta có thể mong đợi. Nhưng trên thực tế, đây là hành vi chính xác cho __. Hai phương thức được gọi là __test [] được tự động đổi tên [bị xáo trộn] thành _a__test [] và _b__test [], do đó chúng không vô tình ghi đè. Khi bạn tạo một phương thức bắt đầu bằng __, điều đó có nghĩa là bạn không muốn bất cứ ai có thể ghi đè nó và bạn chỉ có ý định truy cập nó từ bên trong lớp của chính nó.
Hai dấu gạch dưới ở đầu và cuối:
Khi chúng ta thấy một phương pháp như
class A[object]:
def __test[self]:
print "I'm a test method in class A"
def test[self]:
self.__test[]
a = A[]
a.test[]
# a.__test[] # This fails with an AttributeError
a._A__test[] # Works! We can access the mangled name directly!
0, đừng gọi nó. Đây là một phương pháp mà Python có nghĩa là để gọi, không phải bạn. Hãy xem:>>> name = "test string"
>>> name.__len__[]
11
>>> len[name]
11
>>> number = 10
>>> number.__add__[40]
50
>>> number + 50
60
Luôn có một toán tử hoặc hàm gốc gọi các phương pháp ma thuật này. Đôi khi nó chỉ là một cuộc gọi python hook trong các tình huống cụ thể. Ví dụ:
class A[object]:
def __test[self]:
print "I'm a test method in class A"
def test[self]:
self.__test[]
a = A[]
a.test[]
# a.__test[] # This fails with an AttributeError
a._A__test[] # Works! We can access the mangled name directly!
1 được gọi khi đối tượng được tạo sau khi class A[object]:
def __test[self]:
print "I'm a test method in class A"
def test[self]:
self.__test[]
a = A[]
a.test[]
# a.__test[] # This fails with an AttributeError
a._A__test[] # Works! We can access the mangled name directly!
2 được gọi để xây dựng thể hiện ...Hãy lấy một ví dụ ...
class FalseCalculator[object]:
def __init__[self, number]:
self.number = number
def __add__[self, number]:
return self.number - number
def __sub__[self, number]:
return self.number + number
number = FalseCalculator[20]
print number + 10 # 10
print number - 20 # 40
Để biết thêm chi tiết, xem Hướng dẫn PEP-8. Để biết thêm các phương pháp ma thuật, hãy xem bản PDF này.
1. Sử dụng trong thông dịch viên. Python tự động lưu trữ giá trị của biểu thức cuối cùng trong trình thông dịch cho một biến cụ thể gọi là "_." Bạn cũng có thể gán các giá trị này cho một biến khác nếu bạn muốn. Bạn có thể sử dụng nó như một biến bình thường.
__ Main __ Python là gì?“_” is used in Python:
- __main__ là tên của môi trường nơi chạy mã cấp cao nhất. Mã cấp cao nhất là mô-đun Python do người dùng chỉ định đầu tiên bắt đầu chạy. Đó là cấp độ hàng đầu vì nó nhập tất cả các mô-đun khác mà chương trình cần.
- Việc sử dụng __ tên __ là gì?
- __name__ Đối với mô -đun Namemain đã nhập có giá trị "Namemain", đó là tên của mô -đun dưới dạng chuỗi. Lưu ý: Hầu hết thời gian, môi trường mã cấp cao nhất sẽ là tập lệnh Python mà bạn thực thi và nơi bạn đang nhập các mô-đun khác.
- _ Trong lớp Python là gì?
- Trình thông dịch Python lưu trữ giá trị biểu thức cuối cùng cho biến đặc biệt có tên là '_'. Tính năng này đã được sử dụng trong phiên dịch Cpython tiêu chuẩn trước và bạn cũng có thể sử dụng nó trong các phiên dịch viên Python khác.
- Trong bài viết này, chúng ta sẽ thấy dấu gạch dưới [_] trong Python.
- Double Undercore trước một tên
- Trình thông dịch Python sửa đổi tên biến với ___. Vì vậy, nhiều lần nó sử dụng như một thành viên riêng vì một lớp khác không thể truy cập trực tiếp biến đó. Mục đích chính của __ là chỉ sử dụng biến /phương thức trong lớp nếu bạn muốn sử dụng nó bên ngoài lớp, bạn có thể công khai.
$ python test.py
I'm test method in class A
I'm test method in class A
0class B[A]:
def __test[self]:
print "I'm test method in class B"
b = B[]
b.test[]
1 class A[object]:
def __test[self]:
print "I'm a test method in class A"
def test[self]:
self.__test[]
a = A[]
a.test[]
# a.__test[] # This fails with an AttributeError
a._A__test[] # Works! We can access the mangled name directly!
18class B[A]:
def __test[self]:
print "I'm test method in class B"
b = B[]
b.test[]
3class A[object]:
def __test[self]:
print "I'm a test method in class A"
def test[self]:
self.__test[]
a = A[]
a.test[]
# a.__test[] # This fails with an AttributeError
a._A__test[] # Works! We can access the mangled name directly!
9
$ python test.py
I'm test method in class A
I'm test method in class A
class B[A]:
def __test[self]:
print "I'm test method in class B"
b = B[]
b.test[]
class A[object]:
def __test[self]:
print "I'm a test method in class A"
def test[self]:
self.__test[]
a = A[]
a.test[]
# a.__test[] # This fails with an AttributeError
a._A__test[] # Works! We can access the mangled name directly!
class B[A]:
def __test[self]:
print "I'm test method in class B"
b = B[]
b.test[]
class A[object]:
def __test[self]:
print "I'm a test method in class A"
def test[self]:
self.__test[]
a = A[]
a.test[]
# a.__test[] # This fails with an AttributeError
a._A__test[] # Works! We can access the mangled name directly!
$ python test.py
I'm test method in class A
I'm test method in class A
0$ python test.py
I'm test method in class A
I'm test method in class A
1class A[object]:
def __test[self]:
print "I'm a test method in class A"
def test[self]:
self.__test[]
a = A[]
a.test[]
# a.__test[] # This fails with an AttributeError
a._A__test[] # Works! We can access the mangled name directly!
7class B[A]:
def __test[self]:
print "I'm test method in class B"
b = B[]
b.test[]
3class A[object]:
def __test[self]:
print "I'm a test method in class A"
def test[self]:
self.__test[]
a = A[]
a.test[]
# a.__test[] # This fails with an AttributeError
a._A__test[] # Works! We can access the mangled name directly!
25
$ python test.py
I'm test method in class A
I'm test method in class A
$ python test.py
I'm test method in class A
I'm test method in class A
class A[object]:
def __test[self]:
print "I'm a test method in class A"
def test[self]:
self.__test[]
a = A[]
a.test[]
# a.__test[] # This fails with an AttributeError
a._A__test[] # Works! We can access the mangled name directly!
class B[A]:
def __test[self]:
print "I'm test method in class B"
b = B[]
b.test[]
class A[object]:
def __test[self]:
print "I'm a test method in class A"
def test[self]:
self.__test[]
a = A[]
a.test[]
# a.__test[] # This fails with an AttributeError
a._A__test[] # Works! We can access the mangled name directly!
Double nhấn mạnh trước và sau một cái tên
Tên bắt đầu bằng __ và kết thúc với cùng một phương pháp đặc biệt trong Python. Python cung cấp các phương pháp này để sử dụng làm quá tải toán tử tùy thuộc vào người dùng. Python cung cấp quy ước này để phân biệt giữa chức năng do người dùng xác định với chức năng mô-đun & NBSP;
$ python test.py
I'm test method in class A
I'm test method in class A
0class B[A]:
def __test[self]:
print "I'm test method in class B"
b = B[]
b.test[]
1 class A[object]:
def __test[self]:
print "I'm a test method in class A"
def test[self]:
self.__test[]
a = A[]
a.test[]
# a.__test[] # This fails with an AttributeError
a._A__test[] # Works! We can access the mangled name directly!
30class B[A]:
def __test[self]:
print "I'm test method in class B"
b = B[]
b.test[]
3class A[object]:
def __test[self]:
print "I'm a test method in class A"
def test[self]:
self.__test[]
a = A[]
a.test[]
# a.__test[] # This fails with an AttributeError
a._A__test[] # Works! We can access the mangled name directly!
32Python3
class B[A]:
def __test[self]:
print "I'm test method in class B"
b = B[]
b.test[]
5$ python test.py
I'm test method in class A
I'm test method in class A
1 class A[object]:
def __test[self]:
print "I'm a test method in class A"
def test[self]:
self.__test[]
a = A[]
a.test[]
# a.__test[] # This fails with an AttributeError
a._A__test[] # Works! We can access the mangled name directly!
35class A[object]:
def __test[self]:
print "I'm a test method in class A"
def test[self]:
self.__test[]
a = A[]
a.test[]
# a.__test[] # This fails with an AttributeError
a._A__test[] # Works! We can access the mangled name directly!
36class A[object]:
def __test[self]:
print "I'm a test method in class A"
def test[self]:
self.__test[]
a = A[]
a.test[]
# a.__test[] # This fails with an AttributeError
a._A__test[] # Works! We can access the mangled name directly!
37__ có nghĩa là gì trong Python?
1. Sử dụng trong thông dịch viên. Python tự động lưu trữ giá trị của biểu thức cuối cùng trong trình thông dịch cho một biến cụ thể gọi là "_." Bạn cũng có thể gán các giá trị này cho một biến khác nếu bạn muốn. Bạn có thể sử dụng nó như một biến bình thường.
__ Main __ Python là gì?
__main__ là tên của môi trường nơi chạy mã cấp cao nhất. Mã cấp cao nhất là mô-đun Python do người dùng chỉ định đầu tiên bắt đầu chạy. Đó là cấp độ hàng đầu vì nó nhập tất cả các mô-đun khác mà chương trình cần.
Python3
Việc sử dụng __ tên __ là gì?
$ python test.py
I'm test method in class A
I'm test method in class A
0class B[A]:
def __test[self]:
print "I'm test method in class B"
b = B[]
b.test[]
1 class B[A]:
def __test[self]:
print "I'm test method in class B"
b = B[]
b.test[]
2class B[A]:
def __test[self]:
print "I'm test method in class B"
b = B[]
b.test[]
3class B[A]:
def __test[self]:
print "I'm test method in class B"
b = B[]
b.test[]
4class B[A]:
def __test[self]:
print "I'm test method in class B"
b = B[]
b.test[]
5$ python test.py
I'm test method in class A
I'm test method in class A
1class A[object]:
def __test[self]:
print "I'm a test method in class A"
def test[self]:
self.__test[]
a = A[]
a.test[]
# a.__test[] # This fails with an AttributeError
a._A__test[] # Works! We can access the mangled name directly!
7class B[A]:
def __test[self]:
print "I'm test method in class B"
b = B[]
b.test[]
8class A[object]:
def __test[self]:
print "I'm a test method in class A"
def test[self]:
self.__test[]
a = A[]
a.test[]
# a.__test[] # This fails with an AttributeError
a._A__test[] # Works! We can access the mangled name directly!
9__name__ Đối với mô -đun Namemain đã nhập có giá trị "Namemain", đó là tên của mô -đun dưới dạng chuỗi. Lưu ý: Hầu hết thời gian, môi trường mã cấp cao nhất sẽ là tập lệnh Python mà bạn thực thi và nơi bạn đang nhập các mô-đun khác.
$ python test.py
I'm test method in class A
I'm test method in class A
0$ python test.py
I'm test method in class A
I'm test method in class A
1>>> name = "test string"
>>> name.__len__[]
11
>>> len[name]
11
>>> number = 10
>>> number.__add__[40]
50
>>> number + 50
60
0$ python test.py
I'm test method in class A
I'm test method in class A
0$ python test.py
I'm test method in class A
I'm test method in class A
1class A[object]:
def __test[self]:
print "I'm a test method in class A"
def test[self]:
self.__test[]
a = A[]
a.test[]
# a.__test[] # This fails with an AttributeError
a._A__test[] # Works! We can access the mangled name directly!
7$ python test.py
I'm test method in class A
5class A[object]:
def __test[self]:
print "I'm a test method in class A"
def test[self]:
self.__test[]
a = A[]
a.test[]
# a.__test[] # This fails with an AttributeError
a._A__test[] # Works! We can access the mangled name directly!
9>>> name = "test string"
>>> name.__len__[]
11
>>> len[name]
11
>>> number = 10
>>> number.__add__[40]
50
>>> number + 50
60
6Output:
1
Ví dụ 4: Dấu gạch gạch đơn trước một tên
Dẫn đầu trước khi biến /chức năng /tên phương thức cho người lập trình chỉ ra rằng nó chỉ dành cho sử dụng nội bộ, có thể được sửa đổi bất cứ khi nào lớp muốn. Ở đây đặt tên tiền tố bởi một dấu gạch dưới được coi là không công khai. Nếu chỉ định từ nhập * Tất cả các tên bắt đầu bằng _ sẽ không nhập. Python không chỉ định thực sự riêng tư nên cái này có thể được gọi trực tiếp từ các mô -đun khác nếu nó được chỉ định trong __all__, chúng tôi cũng gọi nó là Private Private & NBSP yếu;from Import * all the names starting with _ will not import. Python does not specify truly private so this one can be called directly from other modules if it is specified in __all__, We also call it weak Private
Python3
$ python test.py
I'm test method in class A
I'm test method in class A
8 >>> name = "test string"
>>> name.__len__[]
11
>>> len[name]
11
>>> number = 10
>>> number.__add__[40]
50
>>> number + 50
60
8$ python test.py
I'm test method in class A
I'm test method in class A
0class B[A]:
def __test[self]:
print "I'm test method in class B"
b = B[]
b.test[]
1 class B[A]:
def __test[self]:
print "I'm test method in class B"
b = B[]
b.test[]
2class B[A]:
def __test[self]:
print "I'm test method in class B"
b = B[]
b.test[]
3class B[A]:
def __test[self]:
print "I'm test method in class B"
b = B[]
b.test[]
4class B[A]:
def __test[self]:
print "I'm test method in class B"
b = B[]
b.test[]
5class B[A]:
def __test[self]:
print "I'm test method in class B"
b = B[]
b.test[]
3class FalseCalculator[object]:
def __init__[self, number]:
self.number = number
def __add__[self, number]:
return self.number - number
def __sub__[self, number]:
return self.number + number
number = FalseCalculator[20]
print number + 10 # 10
print number - 20 # 40
6$ python test.py
I'm test method in class A
I'm test method in class A
6 class A[object]:
def __test[self]:
print "I'm a test method in class A"
def test[self]:
self.__test[]
a = A[]
a.test[]
# a.__test[] # This fails with an AttributeError
a._A__test[] # Works! We can access the mangled name directly!
8class B[A]:
def __test[self]:
print "I'm test method in class B"
b = B[]
b.test[]
5class B[A]:
def __test[self]:
print "I'm test method in class B"
b = B[]
b.test[]
311
$ python test.py
I'm test method in class A
I'm test method in class A
6 13
14
$ python test.py
I'm test method in class A
I'm test method in class A
6 16
$ python test.py
I'm test method in class A
I'm test method in class A
118
$ python test.py
I'm test method in class A
I'm test method in class A
110 120
10 12
Ví dụ 5: Dấu gạch dưới bằng chữ số
Cú pháp Python được sử dụng sao cho dấu gạch dưới có thể được sử dụng làm bộ phân cách trực quan cho các lý do nhóm chữ số để tăng khả năng đọc. Đây là một tính năng điển hình của hầu hết các ngôn ngữ hiện tại và có thể hỗ trợ khả năng đọc của các chữ dài, hoặc nghĩa đen có giá trị nên được phân tách rõ ràng thành các phần.
Python3
10 121
$ python test.py
I'm test method in class A
I'm test method in class A
6 10 123
10 124
10 125
10 126
$ python test.py
I'm test method in class A
I'm test method in class A
6 10 128
10 129
$ python test.py
I'm test method in class A
I'm test method in class A
6 errors
1Double Undercore trước một tên
Double Double Undercore nói với trình thông dịch Python viết lại tên để tránh xung đột trong một lớp con. Thông dịch viên thay đổi tên biến với phần mở rộng lớp và tính năng đó được gọi là Mangling. & NBSP;
Python3
$ python test.py
I'm test method in class A
I'm test method in class A
8 errors
3$ python test.py
I'm test method in class A
I'm test method in class A
0class B[A]:
def __test[self]:
print "I'm test method in class B"
b = B[]
b.test[]
1 class B[A]:
def __test[self]:
print "I'm test method in class B"
b = B[]
b.test[]
2class B[A]:
def __test[self]:
print "I'm test method in class B"
b = B[]
b.test[]
3class B[A]:
def __test[self]:
print "I'm test method in class B"
b = B[]
b.test[]
4class B[A]:
def __test[self]:
print "I'm test method in class B"
b = B[]
b.test[]
5class B[A]:
def __test[self]:
print "I'm test method in class B"
b = B[]
b.test[]
3class FalseCalculator[object]:
def __init__[self, number]:
self.number = number
def __add__[self, number]:
return self.number - number
def __sub__[self, number]:
return self.number + number
number = FalseCalculator[20]
print number + 10 # 10
print number - 20 # 40
6$ python test.py
I'm test method in class A
I'm test method in class A
6 class A[object]:
def __test[self]:
print "I'm a test method in class A"
def test[self]:
self.__test[]
a = A[]
a.test[]
# a.__test[] # This fails with an AttributeError
a._A__test[] # Works! We can access the mangled name directly!
8
5class B[A]:
def __test[self]:
print "I'm test method in class B"
b = B[]
b.test[]
3class B[A]:
def __test[self]:
print "I'm test method in class B"
b = B[]
b.test[]
1
1
6 $ python test.py
I'm test method in class A
I'm test method in class A
1
3
testFile.py
14
$ python test.py
I'm test method in class A
I'm test method in class A
6 16
Python3
$ python test.py
I'm test method in class A
I'm test method in class A
8 errors
3$ python test.py
I'm test method in class A
I'm test method in class A
0class B[A]:
def __test[self]:
print "I'm test method in class B"
b = B[]
b.test[]
1 class B[A]:
def __test[self]:
print "I'm test method in class B"
b = B[]
b.test[]
2class B[A]:
def __test[self]:
print "I'm test method in class B"
b = B[]
b.test[]
3class B[A]:
def __test[self]:
print "I'm test method in class B"
b = B[]
b.test[]
4class B[A]:
def __test[self]:
print "I'm test method in class B"
b = B[]
b.test[]
5class B[A]:
def __test[self]:
print "I'm test method in class B"
b = B[]
b.test[]
3class FalseCalculator[object]:
def __init__[self, number]:
self.number = number
def __add__[self, number]:
return self.number - number
def __sub__[self, number]:
return self.number + number
number = FalseCalculator[20]
print number + 10 # 10
print number - 20 # 40
6$ python test.py
I'm test method in class A
I'm test method in class A
6 class A[object]:
def __test[self]:
print "I'm a test method in class A"
def test[self]:
self.__test[]
a = A[]
a.test[]
# a.__test[] # This fails with an AttributeError
a._A__test[] # Works! We can access the mangled name directly!
8class B[A]:
def __test[self]:
print "I'm test method in class B"
b = B[]
b.test[]
5class B[A]:
def __test[self]:
print "I'm test method in class B"
b = B[]
b.test[]
311
$ python test.py
I'm test method in class A
I'm test method in class A
6 13
14
$ python test.py
I'm test method in class A
I'm test method in class A
6 16
5class B[A]:
def __test[self]:
print "I'm test method in class B"
b = B[]
b.test[]
3class B[A]:
def __test[self]:
print "I'm test method in class B"
b = B[]
b.test[]
1
1
6 $ python test.py
I'm test method in class A
I'm test method in class A
1
3
1
4$ python test.py
I'm test method in class A
I'm test method in class A
6 1
6
$ python test.py
I'm test method in class A
I'm test method in class A
Ví dụ 5: Dấu gạch dưới bằng chữ số
Python3
$ python test.py
I'm test method in class A
I'm test method in class A
8 errors
3class B[A]:
def __test[self]:
print "I'm test method in class B"
b = B[]
b.test[]
5class B[A]:
def __test[self]:
print "I'm test method in class B"
b = B[]
b.test[]
3class A[object]:
def __test[self]:
print "I'm a test method in class A"
def test[self]:
self.__test[]
a = A[]
a.test[]
# a.__test[] # This fails with an AttributeError
a._A__test[] # Works! We can access the mangled name directly!
01$ python test.py
I'm test method in class A
I'm test method in class A
6 class A[object]:
def __test[self]:
print "I'm a test method in class A"
def test[self]:
self.__test[]
a = A[]
a.test[]
# a.__test[] # This fails with an AttributeError
a._A__test[] # Works! We can access the mangled name directly!
8Gọi từ thông dịch viên
5class B[A]:
def __test[self]:
print "I'm test method in class B"
b = B[]
b.test[]
3class B[A]:
def __test[self]:
print "I'm test method in class B"
b = B[]
b.test[]
1
1
6 $ python test.py
I'm test method in class A
I'm test method in class A
1
3