Mặc dù thuộc tính thể hiện trong Python có các đặc điểm và định nghĩa hoàn toàn giống với các ngôn ngữ hướng đối tượng khác, nhưng thuộc tính lớp thường bị coi nhầm là tương đương chính xác với thuộc tính tĩnh . Thuộc tính lớp trong Python và thuộc tính tĩnh trong Java hoặc C++ có nhiều điểm chung, nhưng chúng có một số khác biệt về hành vi mà tôi sẽ nhấn mạnh trong bài viết này. Java or C++. Class attributes in Python and static attributes in Java or C++ have a lot in common, but they have several behavioral differences that I will highlight in this article.
Thuộc tính lớp so với. Thuộc tính phiên bản
Hãy bắt đầu với những điều cơ bản
- Một thuộc tính thể hiện là một biến Python thuộc về một và chỉ một đối tượng. Biến này chỉ có thể truy cập được trong phạm vi của đối tượng này và được định nghĩa bên trong hàm tạo,
1 của lớp.if __name__ == '__main__': foo = ExampleClass[1] bar = ExampleClass[2] # print the instance attribute of the object foo print [foo.istance_attr] #1 #print the instance attribute of the object var print [bar.instance_attr] #2 #print the class attribute of the class ExampleClass as a property of the class itself print [ExampleClass.class_attr] #0 #print the classattribute of the class as a proporty of the objects foo,bar print [bar.class_attr] #0 print [foo.class_attr] #0 # try to print instance attribute as a class property print [ExampleClass.instance_attr] #AttributeError: type object 'ExampleClass' has no attribute 'instance_attr'
- A thuộc tính lớp là một biến Python thuộc về một lớp chứ không phải một đối tượng cụ thể. Nó được chia sẻ giữa tất cả các đối tượng của lớp này và được định nghĩa bên ngoài hàm tạo,
0 , của lớp.if __name__ == '__main__': foo = ExampleClass[1] bar = ExampleClass[2] # print the instance attribute of the object foo print [foo.istance_attr] #1 #print the instance attribute of the object var print [bar.instance_attr] #2 #print the class attribute of the class ExampleClass as a property of the class itself print [ExampleClass.class_attr] #0 #print the classattribute of the class as a proporty of the objects foo,bar print [bar.class_attr] #0 print [foo.class_attr] #0 # try to print instance attribute as a class property print [ExampleClass.instance_attr] #AttributeError: type object 'ExampleClass' has no attribute 'instance_attr'
Dưới đây
if __name__ == '__main__':
foo = ExampleClass[1]
bar = ExampleClass[2]
# print the instance attribute of the object foo
print [foo.istance_attr]
#1
#print the instance attribute of the object var
print [bar.instance_attr]
#2
#print the class attribute of the class ExampleClass as a property of the class itself
print [ExampleClass.class_attr]
#0
#print the classattribute of the class as a proporty of the objects foo,bar
print [bar.class_attr]
#0
print [foo.class_attr]
#0
# try to print instance attribute as a class property
print [ExampleClass.instance_attr]
#AttributeError: type object 'ExampleClass' has no attribute 'instance_attr'
1 là một lớp Python cơ bản với hai thuộc tính. ______02 và if __name__ == '__main__':
foo = ExampleClass[1]
bar = ExampleClass[2]
# print the instance attribute of the object foo
print [foo.istance_attr]
#1
#print the instance attribute of the object var
print [bar.instance_attr]
#2
#print the class attribute of the class ExampleClass as a property of the class itself
print [ExampleClass.class_attr]
#0
#print the classattribute of the class as a proporty of the objects foo,bar
print [bar.class_attr]
#0
print [foo.class_attr]
#0
# try to print instance attribute as a class property
print [ExampleClass.instance_attr]
#AttributeError: type object 'ExampleClass' has no attribute 'instance_attr'
3 . class ExampleClass[object]:
class_attr = 0
def __init__[self, instance_attr]:
self.instance_attr = instance_attr
3 là một thuộc tính thể hiện được định nghĩa bên trong hàm tạo.if __name__ == '__main__': foo = ExampleClass[1] bar = ExampleClass[2] # print the instance attribute of the object foo print [foo.istance_attr] #1 #print the instance attribute of the object var print [bar.instance_attr] #2 #print the class attribute of the class ExampleClass as a property of the class itself print [ExampleClass.class_attr] #0 #print the classattribute of the class as a proporty of the objects foo,bar print [bar.class_attr] #0 print [foo.class_attr] #0 # try to print instance attribute as a class property print [ExampleClass.instance_attr] #AttributeError: type object 'ExampleClass' has no attribute 'instance_attr'
2 là thuộc tính lớp được định nghĩa bên ngoài hàm tạo.if __name__ == '__main__': foo = ExampleClass[1] bar = ExampleClass[2] # print the instance attribute of the object foo print [foo.istance_attr] #1 #print the instance attribute of the object var print [bar.instance_attr] #2 #print the class attribute of the class ExampleClass as a property of the class itself print [ExampleClass.class_attr] #0 #print the classattribute of the class as a proporty of the objects foo,bar print [bar.class_attr] #0 print [foo.class_attr] #0 # try to print instance attribute as a class property print [ExampleClass.instance_attr] #AttributeError: type object 'ExampleClass' has no attribute 'instance_attr'
The
if __name__ == '__main__':
foo = ExampleClass[1]
bar = ExampleClass[2]
# print the instance attribute of the object foo
print [foo.istance_attr]
#1
#print the instance attribute of the object var
print [bar.instance_attr]
#2
#print the class attribute of the class ExampleClass as a property of the class itself
print [ExampleClass.class_attr]
#0
#print the classattribute of the class as a proporty of the objects foo,bar
print [bar.class_attr]
#0
print [foo.class_attr]
#0
# try to print instance attribute as a class property
print [ExampleClass.instance_attr]
#AttributeError: type object 'ExampleClass' has no attribute 'instance_attr'
3 chỉ có thể truy cập được từ phạm vi của một đối tượng. Thuộc tính lớp [ if __name__ == '__main__':
foo = ExampleClass[1]
bar = ExampleClass[2]
# print the instance attribute of the object foo
print [foo.istance_attr]
#1
#print the instance attribute of the object var
print [bar.instance_attr]
#2
#print the class attribute of the class ExampleClass as a property of the class itself
print [ExampleClass.class_attr]
#0
#print the classattribute of the class as a proporty of the objects foo,bar
print [bar.class_attr]
#0
print [foo.class_attr]
#0
# try to print instance attribute as a class property
print [ExampleClass.instance_attr]
#AttributeError: type object 'ExampleClass' has no attribute 'instance_attr'
2 ] có thể truy cập vừa là thuộc tính của lớp vừa là thuộc tính của đối tượng, vì nó được chia sẻ giữa tất cả chúng. if __name__ == '__main__':
foo = ExampleClass[1]
bar = ExampleClass[2]
# print the instance attribute of the object foo
print [foo.istance_attr]
#1
#print the instance attribute of the object var
print [bar.instance_attr]
#2
#print the class attribute of the class ExampleClass as a property of the class itself
print [ExampleClass.class_attr]
#0
#print the classattribute of the class as a proporty of the objects foo,bar
print [bar.class_attr]
#0
print [foo.class_attr]
#0
# try to print instance attribute as a class property
print [ExampleClass.instance_attr]
#AttributeError: type object 'ExampleClass' has no attribute 'instance_attr'
Lưu ý rằng thuộc tính lớp có thể được truy cập dưới dạng thuộc tính lớp và thuộc tính thể hiện, tuy nhiên, việc truy cập thuộc tính thể hiện dưới dạng thuộc tính lớp sẽ làm tăng
if __name__ == '__main__':
foo = ExampleClass[1]
bar = ExampleClass[2]
# print the instance attribute of the object foo
print [foo.istance_attr]
#1
#print the instance attribute of the object var
print [bar.instance_attr]
#2
#print the class attribute of the class ExampleClass as a property of the class itself
print [ExampleClass.class_attr]
#0
#print the classattribute of the class as a proporty of the objects foo,bar
print [bar.class_attr]
#0
print [foo.class_attr]
#0
# try to print instance attribute as a class property
print [ExampleClass.instance_attr]
#AttributeError: type object 'ExampleClass' has no attribute 'instance_attr'
8 .Thông tin thêm về Python. Cách sử dụng Pass, Continue và Break trong Python
Thuộc tính lớp Python. không gian tên
Đằng sau hậu trường, đó là trò chơi không gian tên. Nếu bạn đã đọc Zen of Python, thì dòng cuối cùng cho biết. “Không gian tên là một ý tưởng tuyệt vời — hãy làm nhiều hơn thế nữa. ” Vậy không gian tên là gì?
Trong Python, một không gian tên là ánh xạ giữa các đối tượng và tên. Để đơn giản, giả sử đó là một từ điển Python đại diện cho một khóa cho tên của đối tượng và giá trị của nó dưới dạng giá trị. Các không gian tên khác nhau có thể cùng tồn tại với thuộc tính trong khi các tên bên trong chúng độc lập
Các lớp và đối tượng Python có các không gian tên khác nhau. Ví dụ của chúng tôi, chúng tôi có
if __name__ == '__main__':
foo = ExampleClass[1]
bar = ExampleClass[2]
# print the instance attribute of the object foo
print [foo.istance_attr]
#1
#print the instance attribute of the object var
print [bar.instance_attr]
#2
#print the class attribute of the class ExampleClass as a property of the class itself
print [ExampleClass.class_attr]
#0
#print the classattribute of the class as a proporty of the objects foo,bar
print [bar.class_attr]
#0
print [foo.class_attr]
#0
# try to print instance attribute as a class property
print [ExampleClass.instance_attr]
#AttributeError: type object 'ExampleClass' has no attribute 'instance_attr'
9 làm không gian tên cho lớp của chúng tôi và if __name__ == '__main__':
foo = ExampleClass[1]
bar = ExampleClass[2]
# print the instance attribute of the object foo
print [foo.istance_attr]
#1
#print the instance attribute of the object var
print [bar.instance_attr]
#2
#print the class attribute of the class ExampleClass as a property of the class itself
print [ExampleClass.class_attr]
#0
#print the classattribute of the class as a proporty of the objects foo,bar
print [bar.class_attr]
#0
print [foo.class_attr]
#0
# try to print instance attribute as a class property
print [ExampleClass.instance_attr]
#AttributeError: type object 'ExampleClass' has no attribute 'instance_attr'
20 as a namespace for our object if __name__ == '__main__':
foo = ExampleClass[1]
bar = ExampleClass[2]
# print the instance attribute of the object foo
print [foo.istance_attr]
#1
#print the instance attribute of the object var
print [bar.instance_attr]
#2
#print the class attribute of the class ExampleClass as a property of the class itself
print [ExampleClass.class_attr]
#0
#print the classattribute of the class as a proporty of the objects foo,bar
print [bar.class_attr]
#0
print [foo.class_attr]
#0
# try to print instance attribute as a class property
print [ExampleClass.instance_attr]
#AttributeError: type object 'ExampleClass' has no attribute 'instance_attr'
21.if __name__ == '__main__':
foo = ExampleClass[1]
bar = ExampleClass[2]
# print the instance attribute of the object foo
print [foo.istance_attr]
#1
#print the instance attribute of the object var
print [bar.instance_attr]
#2
#print the class attribute of the class ExampleClass as a property of the class itself
print [ExampleClass.class_attr]
#0
#print the classattribute of the class as a proporty of the objects foo,bar
print [bar.class_attr]
#0
print [foo.class_attr]
#0
# try to print instance attribute as a class property
print [ExampleClass.instance_attr]
#AttributeError: type object 'ExampleClass' has no attribute 'instance_attr'
2Khi bạn truy cập một thuộc tính [thuộc tính thể hiện hoặc lớp] như một thuộc tính của một đối tượng bằng cách sử dụng quy ước dấu chấm, nó sẽ tìm kiếm tên thuộc tính đó trong không gian tên của đối tượng đó trước tiên. Nếu nó được tìm thấy, nó sẽ trả về giá trị. Mặt khác, nó tìm kiếm trong không gian tên của lớp. Nếu không tìm thấy gì ở đó, nó sẽ tăng
if __name__ == '__main__':
foo = ExampleClass[1]
bar = ExampleClass[2]
# print the instance attribute of the object foo
print [foo.istance_attr]
#1
#print the instance attribute of the object var
print [bar.instance_attr]
#2
#print the class attribute of the class ExampleClass as a property of the class itself
print [ExampleClass.class_attr]
#0
#print the classattribute of the class as a proporty of the objects foo,bar
print [bar.class_attr]
#0
print [foo.class_attr]
#0
# try to print instance attribute as a class property
print [ExampleClass.instance_attr]
#AttributeError: type object 'ExampleClass' has no attribute 'instance_attr'
8 . Không gian tên đối tượng nằm trước không gian tên lớp. Nếu chúng tôi tìm thấy, trong một lớp, cả thuộc tính thể hiện và thuộc tính lớp có cùng tên, thì quyền truy cập vào tên đó từ đối tượng của bạn sẽ nhận được giá trị trong không gian tên đối tượng. Dưới đây là phiên bản đơn giản hóa của chức năng tra cứu.
if __name__ == '__main__':
foo = ExampleClass[1]
bar = ExampleClass[2]
# print the instance attribute of the object foo
print [foo.istance_attr]
#1
#print the instance attribute of the object var
print [bar.instance_attr]
#2
#print the class attribute of the class ExampleClass as a property of the class itself
print [ExampleClass.class_attr]
#0
#print the classattribute of the class as a proporty of the objects foo,bar
print [bar.class_attr]
#0
print [foo.class_attr]
#0
# try to print instance attribute as a class property
print [ExampleClass.instance_attr]
#AttributeError: type object 'ExampleClass' has no attribute 'instance_attr'
6Khi bạn truy cập một thuộc tính dưới dạng thuộc tính lớp, nó sẽ tìm kiếm trực tiếp trong không gian tên lớp để tìm tên của thuộc tính đó. Nếu nó được tìm thấy, nó sẽ trả về giá trị. Nếu không, nó sẽ tăng
if __name__ == '__main__':
foo = ExampleClass[1]
bar = ExampleClass[2]
# print the instance attribute of the object foo
print [foo.istance_attr]
#1
#print the instance attribute of the object var
print [bar.instance_attr]
#2
#print the class attribute of the class ExampleClass as a property of the class itself
print [ExampleClass.class_attr]
#0
#print the classattribute of the class as a proporty of the objects foo,bar
print [bar.class_attr]
#0
print [foo.class_attr]
#0
# try to print instance attribute as a class property
print [ExampleClass.instance_attr]
#AttributeError: type object 'ExampleClass' has no attribute 'instance_attr'
8 . Thông tin thêm về Python. 11 Trình chỉnh sửa mã và IDE Python tốt nhất hiện có
Thuộc tính lớp biến đổi thành thuộc tính sơ thẩm
Nó có vẻ kỳ lạ, nhưng các thuộc tính lớp thực sự biến đổi để trở thành các thuộc tính thể hiện. Hãy xem xét tình huống sau đây, rồi cùng nhau nhận xét về nó
if __name__ == '__main__':
foo = ExampleClass[1]
bar = ExampleClass[2]
# print the instance attribute of the object foo
print [foo.istance_attr]
#1
#print the instance attribute of the object var
print [bar.instance_attr]
#2
#print the class attribute of the class ExampleClass as a property of the class itself
print [ExampleClass.class_attr]
#0
#print the classattribute of the class as a proporty of the objects foo,bar
print [bar.class_attr]
#0
print [foo.class_attr]
#0
# try to print instance attribute as a class property
print [ExampleClass.instance_attr]
#AttributeError: type object 'ExampleClass' has no attribute 'instance_attr'
8Các
if __name__ == '__main__':
foo = ExampleClass[1]
bar = ExampleClass[2]
# print the instance attribute of the object foo
print [foo.istance_attr]
#1
#print the instance attribute of the object var
print [bar.instance_attr]
#2
#print the class attribute of the class ExampleClass as a property of the class itself
print [ExampleClass.class_attr]
#0
#print the classattribute of the class as a proporty of the objects foo,bar
print [bar.class_attr]
#0
print [foo.class_attr]
#0
# try to print instance attribute as a class property
print [ExampleClass.instance_attr]
#AttributeError: type object 'ExampleClass' has no attribute 'instance_attr'
2 được chia sẻ giữa tất cả các đối tượng của lớp. Tuy nhiên, khi chúng tôi thay đổi giá trị từ đối tượng foo , thay đổi này không hiển thị từ if __name__ == '__main__':
foo = ExampleClass[1]
bar = ExampleClass[2]
# print the instance attribute of the object foo
print [foo.istance_attr]
#1
#print the instance attribute of the object var
print [bar.instance_attr]
#2
#print the class attribute of the class ExampleClass as a property of the class itself
print [ExampleClass.class_attr]
#0
#print the classattribute of the class as a proporty of the objects foo,bar
print [bar.class_attr]
#0
print [foo.class_attr]
#0
# try to print instance attribute as a class property
print [ExampleClass.instance_attr]
#AttributeError: type object 'ExampleClass' has no attribute 'instance_attr'
25 object, which still has the old value, if __name__ == '__main__':
foo = ExampleClass[1]
bar = ExampleClass[2]
# print the instance attribute of the object foo
print [foo.istance_attr]
#1
#print the instance attribute of the object var
print [bar.instance_attr]
#2
#print the class attribute of the class ExampleClass as a property of the class itself
print [ExampleClass.class_attr]
#0
#print the classattribute of the class as a proporty of the objects foo,bar
print [bar.class_attr]
#0
print [foo.class_attr]
#0
# try to print instance attribute as a class property
print [ExampleClass.instance_attr]
#AttributeError: type object 'ExampleClass' has no attribute 'instance_attr'
26, rather than the new value, if __name__ == '__main__':
foo = ExampleClass[1]
bar = ExampleClass[2]
# print the instance attribute of the object foo
print [foo.istance_attr]
#1
#print the instance attribute of the object var
print [bar.instance_attr]
#2
#print the class attribute of the class ExampleClass as a property of the class itself
print [ExampleClass.class_attr]
#0
#print the classattribute of the class as a proporty of the objects foo,bar
print [bar.class_attr]
#0
print [foo.class_attr]
#0
# try to print instance attribute as a class property
print [ExampleClass.instance_attr]
#AttributeError: type object 'ExampleClass' has no attribute 'instance_attr'
27. Let’s check our namespaces and try to understand what’s happening.if __name__ == '__main__':
foo = ExampleClass[1]
bar = ExampleClass[2]
# print the instance attribute of the object foo
print [foo.istance_attr]
#1
#print the instance attribute of the object var
print [bar.instance_attr]
#2
#print the class attribute of the class ExampleClass as a property of the class itself
print [ExampleClass.class_attr]
#0
#print the classattribute of the class as a proporty of the objects foo,bar
print [bar.class_attr]
#0
print [foo.class_attr]
#0
# try to print instance attribute as a class property
print [ExampleClass.instance_attr]
#AttributeError: type object 'ExampleClass' has no attribute 'instance_attr'
3Ảnh hưởng đã thêm một thuộc tính thể hiện mới vào đối tượng ______128 và chỉ cho đối tượng đó, đó là lý do tại sao trong ví dụ trước, .
if __name__ == '__main__':
foo = ExampleClass[1]
bar = ExampleClass[2]
# print the instance attribute of the object foo
print [foo.istance_attr]
#1
#print the instance attribute of the object var
print [bar.instance_attr]
#2
#print the class attribute of the class ExampleClass as a property of the class itself
print [ExampleClass.class_attr]
#0
#print the classattribute of the class as a proporty of the objects foo,bar
print [bar.class_attr]
#0
print [foo.class_attr]
#0
# try to print instance attribute as a class property
print [ExampleClass.instance_attr]
#AttributeError: type object 'ExampleClass' has no attribute 'instance_attr'
25 kept printing the class attribute.Với các đối tượng không thay đổi, hành vi này luôn giống nhau. Tuy nhiên, với các đối tượng có thể thay đổi như danh sách chẳng hạn, không phải lúc nào cũng vậy và tùy thuộc vào cách bạn sửa đổi thuộc tính lớp của mình.
Hãy thay đổi lớp trước của chúng ta để có một danh sách dưới dạng thuộc tính lớp
if __name__ == '__main__':
foo = ExampleClass[1]
bar = ExampleClass[2]
# print the instance attribute of the object foo
print [foo.istance_attr]
#1
#print the instance attribute of the object var
print [bar.instance_attr]
#2
#print the class attribute of the class ExampleClass as a property of the class itself
print [ExampleClass.class_attr]
#0
#print the classattribute of the class as a proporty of the objects foo,bar
print [bar.class_attr]
#0
print [foo.class_attr]
#0
# try to print instance attribute as a class property
print [ExampleClass.instance_attr]
#AttributeError: type object 'ExampleClass' has no attribute 'instance_attr'
6Chúng tôi sửa đổi danh sách đó dưới dạng thuộc tính của đối tượng
if __name__ == '__main__':
foo = ExampleClass[1]
bar = ExampleClass[2]
# print the instance attribute of the object foo
print [foo.istance_attr]
#1
#print the instance attribute of the object var
print [bar.instance_attr]
#2
#print the class attribute of the class ExampleClass as a property of the class itself
print [ExampleClass.class_attr]
#0
#print the classattribute of the class as a proporty of the objects foo,bar
print [bar.class_attr]
#0
print [foo.class_attr]
#0
# try to print instance attribute as a class property
print [ExampleClass.instance_attr]
#AttributeError: type object 'ExampleClass' has no attribute 'instance_attr'
28 bằng cách nối thêm phần tử mới vào danh sách đó. if __name__ == '__main__':
foo = ExampleClass[1]
bar = ExampleClass[2]
# print the instance attribute of the object foo
print [foo.istance_attr]
#1
#print the instance attribute of the object var
print [bar.instance_attr]
#2
#print the class attribute of the class ExampleClass as a property of the class itself
print [ExampleClass.class_attr]
#0
#print the classattribute of the class as a proporty of the objects foo,bar
print [bar.class_attr]
#0
print [foo.class_attr]
#0
# try to print instance attribute as a class property
print [ExampleClass.instance_attr]
#AttributeError: type object 'ExampleClass' has no attribute 'instance_attr'
8Khi một thuộc tính lớp có thể thay đổi được sửa đổi bởi một đối tượng, nó sẽ không biến đổi thành một thuộc tính thể hiện cho đối tượng đó. Nó vẫn được chia sẻ giữa tất cả các đối tượng của lớp với các phần tử mới được thêm vào nó.
Tuy nhiên, nếu bạn đính kèm một danh sách mới vào thuộc tính đó [
if __name__ == '__main__':
foo = ExampleClass[1]
bar = ExampleClass[2]
# print the instance attribute of the object foo
print [foo.istance_attr]
#1
#print the instance attribute of the object var
print [bar.instance_attr]
#2
#print the class attribute of the class ExampleClass as a property of the class itself
print [ExampleClass.class_attr]
#0
#print the classattribute of the class as a proporty of the objects foo,bar
print [bar.class_attr]
#0
print [foo.class_attr]
#0
# try to print instance attribute as a class property
print [ExampleClass.instance_attr]
#AttributeError: type object 'ExampleClass' has no attribute 'instance_attr'
61 ], bạn sẽ có hành vi tương tự như các đối tượng bất biến. if __name__ == '__main__':
foo = ExampleClass[1]
bar = ExampleClass[2]
# print the instance attribute of the object foo
print [foo.istance_attr]
#1
#print the instance attribute of the object var
print [bar.instance_attr]
#2
#print the class attribute of the class ExampleClass as a property of the class itself
print [ExampleClass.class_attr]
#0
#print the classattribute of the class as a proporty of the objects foo,bar
print [bar.class_attr]
#0
print [foo.class_attr]
#0
# try to print instance attribute as a class property
print [ExampleClass.instance_attr]
#AttributeError: type object 'ExampleClass' has no attribute 'instance_attr'
0Bạn có thể tự mình so sánh các không gian tên để làm bằng chứng cho hành vi trước đó
Điều đáng nói là các thuộc tính lớp Python có thể hữu ích trong các trường hợp khác nhau, tuy nhiên, chúng phải được sử dụng một cách thận trọng để tránh các hành vi không mong muốn
Sự khác biệt giữa thuộc tính lớp và thuộc tính thể hiện là gì?
Sự khác biệt giữa một thuộc tính và một thể hiện là gì?
Thuộc tính cá thể trong Python là gì?
Sự khác biệt giữa các thuộc tính và biến thể hiện là gì?