Một cách khác là gọi hàm dir[]
[xem //docs.python.org/2/l Library/funces.html#dir].
a = Animal[]
dir[a]
>>>
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__',
'__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__',
'__weakref__', 'age', 'color', 'kids', 'legs', 'name', 'smell']
Lưu ý rằng dir[]
cố gắng đạt được bất kỳ thuộc tính nào có thể đạt được.
Sau đó, bạn có thể truy cập các thuộc tính, ví dụ: Bằng cách lọc với Double nhấn mạnh:
attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
Đây chỉ là một ví dụ về những gì có thể làm với dir[]
, vui lòng kiểm tra các câu trả lời khác để biết cách này.
Làm thế nào để tôi thấy tất cả các thuộc tính của một đối tượng trong Python?
Example:
Dir [] Để liệt kê tất cả các thuộc tính của một đối tượng, hãy sử dụng hàm DIR [] tích hợp. Nó trả về một danh sách dài các tên thuộc tính, nghĩa là, phương thức và tên biến của đối tượng.
Làm thế nào để tôi tìm thấy các thuộc tính trong Python?
Chúng ta có thể sử dụng hàm HasAttr [] để tìm nếu một đối tượng Python obj có một thuộc tính hoặc thuộc tính nhất định. Hasattr [obj, 'thuộc tính']: Công ước trong Python là, nếu tài sản có thể ở đó, chỉ cần gọi nó và bắt nó với một khối thử/ngoại trừ.
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ó.
class
Student:
attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
0attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
1attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
2 attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
3____10
attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
5 attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
6attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
7attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
8first second third 2 {'attr': 2} dict_keys[['attr']] dict_values[[2]]3
first second third 2 {'attr': 2} dict_keys[['attr']] dict_values[[2]]4
first second third 2 {'attr': 2} dict_keys[['attr']] dict_values[[2]]3
first second third 2 {'attr': 2} dict_keys[['attr']] dict_values[[2]]6
first second third 2 {'attr': 2} dict_keys[['attr']] dict_values[[2]]3
first second third 2 {'attr': 2} dict_keys[['attr']] dict_values[[2]]8
first second third 2 {'attr': 2} dict_keys[['attr']] dict_values[[2]]3
dir[]
0first second third 2 {'attr': 2} dict_keys[['attr']] dict_values[[2]]3
dir[]
2Đầu ra:
COE COE Shivam Sachin 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.
Nhận danh sách các thuộc tính lớp
Điều quan trọng là phải biết các thuộc tính chúng tôi đang làm việc. Đối với dữ liệu nhỏ, thật dễ dàng để nhớ tên của các thuộc tính nhưng khi làm việc với dữ liệu lớn, rất khó để ghi nhớ tất cả các thuộc tính. May mắn thay, chúng tôi có một số chức năng trong Python có sẵn cho nhiệm vụ này.
Phương pháp 1: Để có được danh sách tất cả các thuộc tính, các phương thức cùng với một số phương thức ma thuật được kế thừa của một lớp, chúng tôi sử dụng một bản tích hợp được gọi là dir[]
. To get the list of all the attributes, methods along with some inherited magic methods of a class, we use a built-in called dir[]
.
Example:
class
dir[]
5
attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
0dir[]
7attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
2 dir[]
9attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
0dir[]
1attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
2 dir[]
3attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
0dir[]
5attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
2 dir[]
7____10
attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
5 attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
6attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
7dir[]
2attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
9attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
7dir[]
5attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
2 dir[]
7____10
attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
5 class
0attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
7class
2
attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
9first second third 2 {'attr': 2} dict_keys[['attr']] dict_values[[2]]3
class
5attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
7class
7attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
7class
9attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
7Student:
1attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
7Student:
3Student:
4
attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
2 Student:
6Student:
7first second third 2 ['attr', 2] ['one', 'first'] ['three', 'third'] ['two', 'second']5
Student:
9
first second third 2 {'attr': 2} dict_keys[['attr']] dict_values[[2]]3
class
5attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
02first second third 2 ['attr', 2] ['one', 'first'] ['three', 'third'] ['two', 'second']5
first second third 2 {'attr': 2} dict_keys[['attr']] dict_values[[2]]3
class
5attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
06attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
07first second third 2 {'attr': 2} dict_keys[['attr']] dict_values[[2]]3
class
5attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
10first second third 2 ['attr', 2] ['one', 'first'] ['three', 'third'] ['two', 'second']5
first second third 2 {'attr': 2} dict_keys[['attr']] dict_values[[2]]3
class
5attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
06attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
15Đầu ra:
Thứ ba thứ hai đầu tiên
Bằng cách truyền đối tượng của lớp ['__ class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', ' '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', ' ',' __subclasshook__ ',' __weakref__ ',' attr ',' one ',' show ',' ba ',' hai ']]
[‘__class__’, ‘__delattr__’, ‘__dict__’, ‘__dir__’, ‘__doc__’, ‘__eq__’, ‘__format__’, ‘__ge__’, ‘__getattribute__’, ‘__gt__’, ‘__hash__’, ‘__init__’, ‘__init_subclass__’, ‘__le__’, ‘__lt__’, ‘__module__’, ‘__ne__’, ‘__new__’, ‘__reduce__’, ‘__reduce_ex__’, ‘__repr__’, ‘__setattr__’, ‘__sizeof__’, ‘__str__’, ‘__subclasshook__’, ‘__weakref__’, ‘attr’, ‘one’, ‘show’, ‘three’, ‘two’]Bằng cách vượt qua lớp ['__ class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', ' __init__ ',' __init_subClass__ ',' __le__ ',' __lt__ ',' __module__ ',' __ne__ ',' __new__ ',' __reduce__ ',' , '__subclasshook__', '__weakref__', 'one', 'show', 'ba', 'hai']]]]]
[‘__class__’, ‘__delattr__’, ‘__dict__’, ‘__dir__’, ‘__doc__’, ‘__eq__’, ‘__format__’, ‘__ge__’, ‘__getattribute__’, ‘__gt__’, ‘__hash__’, ‘__init__’, ‘__init_subclass__’, ‘__le__’, ‘__lt__’, ‘__module__’, ‘__ne__’, ‘__new__’, ‘__reduce__’, ‘__reduce_ex__’, ‘__repr__’, ‘__setattr__’, ‘__sizeof__’, ‘__str__’, ‘__subclasshook__’, ‘__weakref__’, ‘one’, ‘show’, ‘three’, ‘two’]
Phương pháp 2: Một cách khác để tìm danh sách các thuộc tính là sử dụng mô -đun
16. Mô -đun này cung cấp một phương thức gọi là attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
17 trả về danh sách các thuộc tính và phương thức lớp. Another way of finding a list of attributes is by using the module attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
16. This module provides a method called attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
17 that returns a list of class attributes and methods.Ví dụ 1:
attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
18 attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
16class
dir[]
5
attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
0dir[]
7attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
2 dir[]
9attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
0dir[]
1attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
2 dir[]
3attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
0dir[]
5attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
2 dir[]
7____10
attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
5 attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
6attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
7dir[]
2attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
9attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
7dir[]
5attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
2 dir[]
7____10
attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
5 class
0attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
7class
2attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
9first second third 2 {'attr': 2} dict_keys[['attr']] dict_values[[2]]3
class
5attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
7class
7attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
7class
9attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
7Student:
1attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
7Student:
3Student:
4
attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
2 Student:
6Student:
7first second third 2 ['attr', 2] ['one', 'first'] ['three', 'third'] ['two', 'second']5
Student:
9
Thứ ba thứ hai đầu tiên
Bằng cách truyền đối tượng của lớp ['__ class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', ' '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', ' ',' __subclasshook__ ',' __weakref__ ',' attr ',' one ',' show ',' ba ',' hai ']]
Bằng cách vượt qua lớp ['__ class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', ' __init__ ',' __init_subClass__ ',' __le__ ',' __lt__ ',' __module__ ',' __ne__ ',' __new__ ',' __reduce__ ',' , '__subclasshook__', '__weakref__', 'one', 'show', 'ba', 'hai']]]]]
attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
84first second third 2 {'attr': 2} dict_keys[['attr']] dict_values[[2]]3
attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
86Đầu ra:
first second third 2 ['attr', 2] ['one', 'first'] ['three', 'third'] ['two', 'second']
Phương pháp 2: Một cách khác để tìm danh sách các thuộc tính là sử dụng mô -đun
16. Mô -đun này cung cấp một phương thức gọi là attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
17 trả về danh sách các thuộc tính và phương thức lớp. To find attributes we can also use magic method attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
87. This method only returns instance attributes.Example:
class
dir[]
5
attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
0dir[]
7attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
2 dir[]
9attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
0dir[]
1attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
2 dir[]
3attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
0dir[]
5attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
2 dir[]
7____10
attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
5 attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
6attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
7dir[]
2attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
9attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
7dir[]
5attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
2 dir[]
7____10
attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
5 class
0attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
7class
2attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
9first second third 2 {'attr': 2} dict_keys[['attr']] dict_values[[2]]3
class
5attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
7class
7attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
7class
9attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
7Student:
1attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
7Student:
3Student:
4
attributes = [attr for attr in dir[a]
if not attr.startswith['__']]
2 Student:
6Student:
7first second third 2 ['attr', 2] ['one', 'first'] ['three', 'third'] ['two', 'second']5
Student:
9
first second third 2 {'attr': 2} dict_keys[['attr']] dict_values[[2]]3
COE COE Shivam Sachin COE35
first second third 2 {'attr': 2} dict_keys[['attr']] dict_values[[2]]3
COE COE Shivam Sachin COE37
first second third 2 {'attr': 2} dict_keys[['attr']] dict_values[[2]]3
COE COE Shivam Sachin COE39
Output:
first second third 2 {'attr': 2} dict_keys[['attr']] dict_values[[2]]