Mở rộng câu trả lời của Alex một chút:
class User: def __init__(self): self.data = [1,2,3] self.other_data = [4,5,6] def doSomething(self, source): dataSource = getattr(self,source) return dataSource A = User() print A.doSomething("data") print A.doSomething("other_data")sẽ mang lại:
[1, 2, 3] [4, 5, 6]Tuy nhiên, cá nhân tôi không nghĩ rằng đó là phong cách tuyệt vời - getattr sẽ cho phép bạn truy cập bất kỳ thuộc tính nào của trường hợp, bao gồm cả những thứ như phương thức doSomething hoặc thậm chí là ____10 của trường hợp. Tôi sẽ đề nghị rằng thay vào đó bạn thực hiện một từ điển của các nguồn dữ liệu, như vậy:
class User: def __init__(self): self.data_sources = { "data": [1,2,3], "other_data":[4,5,6], } def doSomething(self, source): dataSource = self.data_sources[source] return dataSource A = User() print A.doSomething("data") print A.doSomething("other_data")một lần nữa mang lại:
[1, 2, 3] [4, 5, 6]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ó. & NBSP;
Example:
Python3
[1, 2, 3] [4, 5, 6] 1 [1, 2, 3] [4, 5, 6] 2
[1, 2, 3] [4, 5, 6] 3[1, 2, 3] [4, 5, 6] 4[1, 2, 3] [4, 5, 6] 5 [1, 2, 3] [4, 5, 6] 6
[1, 2, 3] [4, 5, 6] 3[1, 2, 3] [4, 5, 6] 8 [1, 2, 3] [4, 5, 6] 9class User: def __init__(self): self.data_sources = { "data": [1,2,3], "other_data":[4,5,6], } def doSomething(self, source): dataSource = self.data_sources[source] return dataSource A = User() print A.doSomething("data") print A.doSomething("other_data") 0class User: def __init__(self): self.data_sources = { "data": [1,2,3], "other_data":[4,5,6], } def doSomething(self, source): dataSource = self.data_sources[source] return dataSource A = User() print A.doSomething("data") print A.doSomething("other_data") 1
class User: def __init__(self): self.data_sources = { "data": [1,2,3], "other_data":[4,5,6], } def doSomething(self, source): dataSource = self.data_sources[source] return dataSource A = User() print A.doSomething("data") print A.doSomething("other_data") 2class User: def __init__(self): self.data_sources = { "data": [1,2,3], "other_data":[4,5,6], } def doSomething(self, source): dataSource = self.data_sources[source] return dataSource A = User() print A.doSomething("data") print A.doSomething("other_data") 0class User: def __init__(self): self.data_sources = { "data": [1,2,3], "other_data":[4,5,6], } def doSomething(self, source): dataSource = self.data_sources[source] return dataSource A = User() print A.doSomething("data") print A.doSomething("other_data") 4[1, 2, 3] [4, 5, 6] 5 class User: def __init__(self): self.data_sources = { "data": [1,2,3], "other_data":[4,5,6], } def doSomething(self, source): dataSource = self.data_sources[source] return dataSource A = User() print A.doSomething("data") print A.doSomething("other_data") 6
class User: def __init__(self): self.data_sources = { "data": [1,2,3], "other_data":[4,5,6], } def doSomething(self, source): dataSource = self.data_sources[source] return dataSource A = User() print A.doSomething("data") print A.doSomething("other_data") 2class User: def __init__(self): self.data_sources = { "data": [1,2,3], "other_data":[4,5,6], } def doSomething(self, source): dataSource = self.data_sources[source] return dataSource A = User() print A.doSomething("data") print A.doSomething("other_data") 0class User: def __init__(self): self.data_sources = { "data": [1,2,3], "other_data":[4,5,6], } def doSomething(self, source): dataSource = self.data_sources[source] return dataSource A = User() print A.doSomething("data") print A.doSomething("other_data") 9[1, 2, 3] [4, 5, 6] 5 [1, 2, 3] [4, 5, 6] 1
[1, 2, 3] [4, 5, 6] 2[1, 2, 3] [4, 5, 6] 5 [1, 2, 3] [4, 5, 6] 4[1, 2, 3] [4, 5, 6] 5[1, 2, 3] [4, 5, 6] 6[1, 2, 3] [4, 5, 6] 7[1, 2, 3] [4, 5, 6] 8
[1, 2, 3] [4, 5, 6] 9[1, 2, 3] [4, 5, 6] 5 [1, 2, 3] [4, 5, 6] 4COE COE Shivam Sachin COE2[1, 2, 3] [4, 5, 6] 6COE COE Shivam Sachin COE4[1, 2, 3] [4, 5, 6] 8
COE COE Shivam Sachin COE6COE COE Shivam Sachin COE7
COE COE Shivam Sachin COE6COE COE Shivam Sachin COE9
COE COE Shivam Sachin COE6first second third 2 ('attr', 2) ('one', 'first') ('three', 'third') ('two', 'second')1
COE COE Shivam Sachin COE6first second third 2 ('attr', 2) ('one', 'first') ('three', 'third') ('two', 'second')3
COE COE Shivam Sachin COE6first second third 2 ('attr', 2) ('one', 'first') ('three', 'third') ('two', 'second')5
Đầu ra: & nbsp;
COE COE Shivam Sachin COELư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. & NBSP;
Sử dụng hàm dir () tích hợp
Để 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 ().dir().
Example: Example:
Python3
[1, 2, 3] [4, 5, 6] 1 first second third 2 ('attr', 2) ('one', 'first') ('three', 'third') ('two', 'second')7
first second third 2 ('attr', 2) ('one', 'first') ('three', 'third') ('two', 'second')8first second third 2 ('attr', 2) ('one', 'first') ('three', 'third') ('two', 'second')9[1, 2, 3] [4, 5, 6] 5 first second third 2 {'attr': 2} dict_keys(['attr']) dict_values([2])1
first second third 2 ('attr', 2) ('one', 'first') ('three', 'third') ('two', 'second')8first second third 2 {'attr': 2} dict_keys(['attr']) dict_values([2])3[1, 2, 3] [4, 5, 6] 5 first second third 2 {'attr': 2} dict_keys(['attr']) dict_values([2])5
first second third 2 ('attr', 2) ('one', 'first') ('three', 'third') ('two', 'second')8first second third 2 {'attr': 2} dict_keys(['attr']) dict_values([2])7[1, 2, 3] [4, 5, 6] 5 first second third 2 {'attr': 2} dict_keys(['attr']) dict_values([2])9
first second third 2 ('attr', 2) ('one', 'first') ('three', 'third') ('two', 'second')8[1, 2, 3] [4, 5, 6] 8 [1, 2, 3] [4, 5, 6] 9class User: def __init__(self): self.data_sources = { "data": [1,2,3], "other_data":[4,5,6], } def doSomething(self, source): dataSource = self.data_sources[source] return dataSource A = User() print A.doSomething("data") print A.doSomething("other_data") 0first second third 2 {'attr': 2}4
‘
first second third 2 ('attr', 2) ('one', 'first') ('three', 'third') ('two', 'second')8[1, 2, 3] [4, 5, 6] 8 getattr2class User: def __init__(self): self.data_sources = { "data": [1,2,3], "other_data":[4,5,6], } def doSomething(self, source): dataSource = self.data_sources[source] return dataSource A = User() print A.doSomething("data") print A.doSomething("other_data") 0getattr4
first second third 2 {'attr': 2}5COE COE Shivam Sachin COE6getattr7class User: def __init__(self): self.data_sources = { "data": [1,2,3], "other_data":[4,5,6], } def doSomething(self, source): dataSource = self.data_sources[source] return dataSource A = User() print A.doSomething("data") print A.doSomething("other_data") 0getattr9class User: def __init__(self): self.data_sources = { "data": [1,2,3], "other_data":[4,5,6], } def doSomething(self, source): dataSource = self.data_sources[source] return dataSource A = User() print A.doSomething("data") print A.doSomething("other_data") 0doSomething1
doSomething2class User: def __init__(self): self.data_sources = { "data": [1,2,3], "other_data":[4,5,6], } def doSomething(self, source): dataSource = self.data_sources[source] return dataSource A = User() print A.doSomething("data") print A.doSomething("other_data") 0doSomething4class User: def __init__(self): self.data_sources = { "data": [1,2,3], "other_data":[4,5,6], } def doSomething(self, source): dataSource = self.data_sources[source] return dataSource A = User() print A.doSomething("data") print A.doSomething("other_data") 0doSomething6
doSomething7[1, 2, 3] [4, 5, 6] 5 doSomething9[1, 2, 3] [4, 5, 6] 00[1, 2, 3] [4, 5, 6] 8
[1, 2, 3] [4, 5, 6] 02
COE COE Shivam Sachin COE6getattr7[1, 2, 3] [4, 5, 6] 05[1, 2, 3] [4, 5, 6] 8
COE COE Shivam Sachin COE6getattr7[1, 2, 3] [4, 5, 6] 09[1, 2, 3] [4, 5, 6] 10
COE COE Shivam Sachin COE6getattr7[1, 2, 3] [4, 5, 6] 13[1, 2, 3] [4, 5, 6] 8
COE COE Shivam Sachin COE6getattr7[1, 2, 3] [4, 5, 6] 09[1, 2, 3] [4, 5, 6] 18
Đầu ra: & nbsp;
Lưu ý: Để biết thêm thông tin, hãy tham khảo các lớp và đối tượng Python.
Sử dụng phương thức GetMemebers ()
Một cách khác để tìm một danh sách các thuộc tính là bằng cách sử dụng kiểm tra mô -đun. Mô -đun này cung cấp một phương thức gọi là getMemebers () trả về danh sách các thuộc tính và phương thức lớp.inspect. This module provides a method called getmemebers() that returns a list of class attributes and methods.
Example:
Python3
[1, 2, 3] [4, 5, 6] 19 [1, 2, 3] [4, 5, 6] 20
[1, 2, 3] [4, 5, 6] 1 first second third 2 ('attr', 2) ('one', 'first') ('three', 'third') ('two', 'second')7
first second third 2 ('attr', 2) ('one', 'first') ('three', 'third') ('two', 'second')8first second third 2 ('attr', 2) ('one', 'first') ('three', 'third') ('two', 'second')9[1, 2, 3] [4, 5, 6] 5 first second third 2 {'attr': 2} dict_keys(['attr']) dict_values([2])1
first second third 2 ('attr', 2) ('one', 'first') ('three', 'third') ('two', 'second')8first second third 2 {'attr': 2} dict_keys(['attr']) dict_values([2])3[1, 2, 3] [4, 5, 6] 5 first second third 2 {'attr': 2} dict_keys(['attr']) dict_values([2])5
first second third 2 ('attr', 2) ('one', 'first') ('three', 'third') ('two', 'second')8first second third 2 {'attr': 2} dict_keys(['attr']) dict_values([2])7[1, 2, 3] [4, 5, 6] 5 first second third 2 {'attr': 2} dict_keys(['attr']) dict_values([2])9
first second third 2 ('attr', 2) ('one', 'first') ('three', 'third') ('two', 'second')8[1, 2, 3] [4, 5, 6] 8 [1, 2, 3] [4, 5, 6] 9class User: def __init__(self): self.data_sources = { "data": [1,2,3], "other_data":[4,5,6], } def doSomething(self, source): dataSource = self.data_sources[source] return dataSource A = User() print A.doSomething("data") print A.doSomething("other_data") 0first second third 2 {'attr': 2}4
‘
first second third 2 ('attr', 2) ('one', 'first') ('three', 'third') ('two', 'second')8[1, 2, 3] [4, 5, 6] 8 getattr2class User: def __init__(self): self.data_sources = { "data": [1,2,3], "other_data":[4,5,6], } def doSomething(self, source): dataSource = self.data_sources[source] return dataSource A = User() print A.doSomething("data") print A.doSomething("other_data") 0getattr4
first second third 2 {'attr': 2}5COE COE Shivam Sachin COE6getattr7class User: def __init__(self): self.data_sources = { "data": [1,2,3], "other_data":[4,5,6], } def doSomething(self, source): dataSource = self.data_sources[source] return dataSource A = User() print A.doSomething("data") print A.doSomething("other_data") 0getattr9class User: def __init__(self): self.data_sources = { "data": [1,2,3], "other_data":[4,5,6], } def doSomething(self, source): dataSource = self.data_sources[source] return dataSource A = User() print A.doSomething("data") print A.doSomething("other_data") 0doSomething1
doSomething2class User: def __init__(self): self.data_sources = { "data": [1,2,3], "other_data":[4,5,6], } def doSomething(self, source): dataSource = self.data_sources[source] return dataSource A = User() print A.doSomething("data") print A.doSomething("other_data") 0doSomething4class User: def __init__(self): self.data_sources = { "data": [1,2,3], "other_data":[4,5,6], } def doSomething(self, source): dataSource = self.data_sources[source] return dataSource A = User() print A.doSomething("data") print A.doSomething("other_data") 0doSomething6
doSomething7[1, 2, 3] [4, 5, 6] 5 doSomething9[1, 2, 3] [4, 5, 6] 00[1, 2, 3] [4, 5, 6] 8
[1, 2, 3] [4, 5, 6] 02
[1, 2, 3] [4, 5, 6] 68 [1, 2, 3] [4, 5, 6] 69[1, 2, 3] [4, 5, 6] 70 [1, 2, 3] [4, 5, 6] 71
first second third 2 ('attr', 2) ('one', 'first') ('three', 'third') ('two', 'second')8[1, 2, 3] [4, 5, 6] 73 [1, 2, 3] [4, 5, 6] 74 [1, 2, 3] [4, 5, 6] 75[1, 2, 3] [4, 5, 6] 76[1, 2, 3] [4, 5, 6] 777____178getattr4
first second third 2 {'attr': 2}5[1, 2, 3] [4, 5, 6] 73 [1, 2, 3] [4, 5, 6] 74 [1, 2, 3] [4, 5, 6] 83[1, 2, 3] [4, 5, 6] 84[1, 2, 3] [4, 5, 6] 85
[1, 2, 3] [4, 5, 6] 86COE COE Shivam Sachin COE6[1, 2, 3] [4, 5, 6] 88
Đầu ra: & nbsp;
first second third 2 ('attr', 2) ('one', 'first') ('three', 'third') ('two', 'second')Sử dụng & nbsp; __ dict __ () Phương pháp ma thuật
Để tìm các thuộc tính, chúng ta cũng có thể sử dụng Phương pháp ma thuật __dict__. Phương thức này chỉ trả về các thuộc tính thể hiện.__dict__. This method only returns instance attributes.
Example:
Python3
[1, 2, 3] [4, 5, 6] 1 first second third 2 ('attr', 2) ('one', 'first') ('three', 'third') ('two', 'second')7
first second third 2 ('attr', 2) ('one', 'first') ('three', 'third') ('two', 'second')8first second third 2 ('attr', 2) ('one', 'first') ('three', 'third') ('two', 'second')9[1, 2, 3] [4, 5, 6] 5 first second third 2 {'attr': 2} dict_keys(['attr']) dict_values([2])1
first second third 2 ('attr', 2) ('one', 'first') ('three', 'third') ('two', 'second')8first second third 2 {'attr': 2} dict_keys(['attr']) dict_values([2])3[1, 2, 3] [4, 5, 6] 5 first second third 2 {'attr': 2} dict_keys(['attr']) dict_values([2])5
first second third 2 ('attr', 2) ('one', 'first') ('three', 'third') ('two', 'second')8first second third 2 {'attr': 2} dict_keys(['attr']) dict_values([2])7[1, 2, 3] [4, 5, 6] 5 first second third 2 {'attr': 2} dict_keys(['attr']) dict_values([2])9
first second third 2 ('attr', 2) ('one', 'first') ('three', 'third') ('two', 'second')8[1, 2, 3] [4, 5, 6] 8 [1, 2, 3] [4, 5, 6] 9class User: def __init__(self): self.data_sources = { "data": [1,2,3], "other_data":[4,5,6], } def doSomething(self, source): dataSource = self.data_sources[source] return dataSource A = User() print A.doSomething("data") print A.doSomething("other_data") 0first second third 2 {'attr': 2}4
‘
first second third 2 ('attr', 2) ('one', 'first') ('three', 'third') ('two', 'second')8[1, 2, 3] [4, 5, 6] 8 getattr2class User: def __init__(self): self.data_sources = { "data": [1,2,3], "other_data":[4,5,6], } def doSomething(self, source): dataSource = self.data_sources[source] return dataSource A = User() print A.doSomething("data") print A.doSomething("other_data") 0getattr4
first second third 2 {'attr': 2}5COE COE Shivam Sachin COE6getattr7class User: def __init__(self): self.data_sources = { "data": [1,2,3], "other_data":[4,5,6], } def doSomething(self, source): dataSource = self.data_sources[source] return dataSource A = User() print A.doSomething("data") print A.doSomething("other_data") 0getattr9class User: def __init__(self): self.data_sources = { "data": [1,2,3], "other_data":[4,5,6], } def doSomething(self, source): dataSource = self.data_sources[source] return dataSource A = User() print A.doSomething("data") print A.doSomething("other_data") 0doSomething1
doSomething2class User: def __init__(self): self.data_sources = { "data": [1,2,3], "other_data":[4,5,6], } def doSomething(self, source): dataSource = self.data_sources[source] return dataSource A = User() print A.doSomething("data") print A.doSomething("other_data") 0doSomething4class User: def __init__(self): self.data_sources = { "data": [1,2,3], "other_data":[4,5,6], } def doSomething(self, source): dataSource = self.data_sources[source] return dataSource A = User() print A.doSomething("data") print A.doSomething("other_data") 0doSomething6
doSomething7[1, 2, 3] [4, 5, 6] 5 doSomething9[1, 2, 3] [4, 5, 6] 00[1, 2, 3] [4, 5, 6] 8
[1, 2, 3] [4, 5, 6] 02
COE COE Shivam Sachin COE6class User: def __init__(self): self.data_sources = { "data": [1,2,3], "other_data":[4,5,6], } def doSomething(self, source): dataSource = self.data_sources[source] return dataSource A = User() print A.doSomething("data") print A.doSomething("other_data") 37
COE COE Shivam Sachin COE6class User: def __init__(self): self.data_sources = { "data": [1,2,3], "other_data":[4,5,6], } def doSomething(self, source): dataSource = self.data_sources[source] return dataSource A = User() print A.doSomething("data") print A.doSomething("other_data") 39
COE COE Shivam Sachin COE6class User: def __init__(self): self.data_sources = { "data": [1,2,3], "other_data":[4,5,6], } def doSomething(self, source): dataSource = self.data_sources[source] return dataSource A = User() print A.doSomething("data") print A.doSomething("other_data") 41
Output:
first second third 2 {'attr': 2} dict_keys(['attr']) dict_values([2])[1, 2, 3] [4, 5, 6] 68 [1, 2, 3] [4, 5, 6] 69[1, 2, 3] [4, 5, 6] 70 [1, 2, 3] [4, 5, 6] 71
first second third 2 ('attr', 2) ('one', 'first') ('three', 'third') ('two', 'second')8[1, 2, 3] [4, 5, 6] 73 [1, 2, 3] [4, 5, 6] 74 [1, 2, 3] [4, 5, 6] 75[1, 2, 3] [4, 5, 6] 76[1, 2, 3] [4, 5, 6] 777____178getattr4
Python3
[1, 2, 3] [4, 5, 6] 19 [1, 2, 3] [4, 5, 6] 20
[1, 2, 3] [4, 5, 6] 1 first second third 2 ('attr', 2) ('one', 'first') ('three', 'third') ('two', 'second')7
first second third 2 ('attr', 2) ('one', 'first') ('three', 'third') ('two', 'second')8first second third 2 ('attr', 2) ('one', 'first') ('three', 'third') ('two', 'second')9[1, 2, 3] [4, 5, 6] 5 first second third 2 {'attr': 2} dict_keys(['attr']) dict_values([2])1
first second third 2 ('attr', 2) ('one', 'first') ('three', 'third') ('two', 'second')8first second third 2 {'attr': 2} dict_keys(['attr']) dict_values([2])3[1, 2, 3] [4, 5, 6] 5 first second third 2 {'attr': 2} dict_keys(['attr']) dict_values([2])5
first second third 2 ('attr', 2) ('one', 'first') ('three', 'third') ('two', 'second')8first second third 2 {'attr': 2} dict_keys(['attr']) dict_values([2])7[1, 2, 3] [4, 5, 6] 5 first second third 2 {'attr': 2} dict_keys(['attr']) dict_values([2])9
first second third 2 ('attr', 2) ('one', 'first') ('three', 'third') ('two', 'second')8[1, 2, 3] [4, 5, 6] 8 [1, 2, 3] [4, 5, 6] 9class User: def __init__(self): self.data_sources = { "data": [1,2,3], "other_data":[4,5,6], } def doSomething(self, source): dataSource = self.data_sources[source] return dataSource A = User() print A.doSomething("data") print A.doSomething("other_data") 0first second third 2 {'attr': 2}4
‘
first second third 2 ('attr', 2) ('one', 'first') ('three', 'third') ('two', 'second')8[1, 2, 3] [4, 5, 6] 8 getattr2class User: def __init__(self): self.data_sources = { "data": [1,2,3], "other_data":[4,5,6], } def doSomething(self, source): dataSource = self.data_sources[source] return dataSource A = User() print A.doSomething("data") print A.doSomething("other_data") 0getattr4
first second third 2 {'attr': 2}5COE COE Shivam Sachin COE6getattr7class User: def __init__(self): self.data_sources = { "data": [1,2,3], "other_data":[4,5,6], } def doSomething(self, source): dataSource = self.data_sources[source] return dataSource A = User() print A.doSomething("data") print A.doSomething("other_data") 0getattr9class User: def __init__(self): self.data_sources = { "data": [1,2,3], "other_data":[4,5,6], } def doSomething(self, source): dataSource = self.data_sources[source] return dataSource A = User() print A.doSomething("data") print A.doSomething("other_data") 0doSomething1
doSomething7[1, 2, 3] [4, 5, 6] 5 doSomething9[1, 2, 3] [4, 5, 6] 00[1, 2, 3] [4, 5, 6] 8
doSomething7[1, 2, 3] [4, 5, 6] 5 doSomething9[1, 2, 3] [4, 5, 6] 00[1, 2, 3] [4, 5, 6] 8
[1, 2, 3] [4, 5, 6] 02
COE COE Shivam Sachin COE6getattr7class User: def __init__(self): self.data_sources = { "data": [1,2,3], "other_data":[4,5,6], } def doSomething(self, source): dataSource = self.data_sources[source] return dataSource A = User() print A.doSomething("data") print A.doSomething("other_data") 93[1, 2, 3] [4, 5, 6] 10
Output:
first second third 2 {'attr': 2}