Hướng dẫn python get class attributes by name - python nhận thuộc tính lớp theo tên

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]
9
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")
0
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")
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")
2
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")
0
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")
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")
2
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")
0
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")
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]
4
COE
COE
Shivam
Sachin
COE
2
[1, 2, 3]
[4, 5, 6]
6
COE
COE
Shivam
Sachin
COE
4
[1, 2, 3]
[4, 5, 6]
8

COE
COE
Shivam
Sachin
COE
6
COE
COE
Shivam
Sachin
COE
7

COE
COE
Shivam
Sachin
COE
6
COE
COE
Shivam
Sachin
COE
9

COE
COE
Shivam
Sachin
COE
6
first second third 2
('attr', 2)
('one', 'first')
('three', 'third')
('two', 'second')
1

COE
COE
Shivam
Sachin
COE
6
first second third 2
('attr', 2)
('one', 'first')
('three', 'third')
('two', 'second')
3

COE
COE
Shivam
Sachin
COE
6
first second third 2
('attr', 2)
('one', 'first')
('three', 'third')
('two', 'second')
5

Đầu ra: & nbsp;

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. & 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')
8
first 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')
8
first 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')
8
first 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]
9
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")
0
first 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 getattr2
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")
0getattr4

first second third 2
{'attr': 2}
5
COE
COE
Shivam
Sachin
COE
6getattr7
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")
0getattr9
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")
0doSomething1

doSomething2

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")
0doSomething4
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")
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
COE
6getattr7
[1, 2, 3]
[4, 5, 6]
05
[1, 2, 3]
[4, 5, 6]
8

COE
COE
Shivam
Sachin
COE
6getattr7
[1, 2, 3]
[4, 5, 6]
09
[1, 2, 3]
[4, 5, 6]
10

COE
COE
Shivam
Sachin
COE
6getattr7
[1, 2, 3]
[4, 5, 6]
13
[1, 2, 3]
[4, 5, 6]
8

COE
COE
Shivam
Sachin
COE
6getattr7
[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')
8
first 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')
8
first 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')
8
first 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]
9
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")
0
first 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 getattr2
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")
0getattr4

first second third 2
{'attr': 2}
5
COE
COE
Shivam
Sachin
COE
6getattr7
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")
0getattr9
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")
0doSomething1

doSomething2

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")
0doSomething4
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")
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]
86
COE
COE
Shivam
Sachin
COE
6
[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')
8
first 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')
8
first 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')
8
first 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]
9
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")
0
first 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 getattr2
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")
0getattr4

first second third 2
{'attr': 2}
5
COE
COE
Shivam
Sachin
COE
6getattr7
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")
0getattr9
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")
0doSomething1

doSomething2

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")
0doSomething4
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")
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
COE
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")
37

COE
COE
Shivam
Sachin
COE
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")
39

COE
COE
Shivam
Sachin
COE
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")
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')
8
first 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')
8
first 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')
8
first 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]
9
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")
0
first 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 getattr2
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")
0getattr4

first second third 2
{'attr': 2}
5
COE
COE
Shivam
Sachin
COE
6getattr7
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")
0getattr9
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")
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
COE
6getattr7
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")
93
[1, 2, 3]
[4, 5, 6]
10

Output:

first second third 2
{'attr': 2}

Làm thế nào để tôi có được thuộc tính của một lớp trong Python?

Các thuộc tính của một lớp cũng có thể được truy cập bằng các phương thức và hàm tích hợp sau: getattr ()-hàm này được sử dụng để truy cập thuộc tính của đối tượng. HasAttr () - Hàm này được sử dụng để kiểm tra xem thuộc tính có tồn tại hay không. setAttr () - Hàm này được sử dụng để đặt thuộc tính.getattr() – This function is used to access the attribute of object. hasattr() – This function is used to check if an attribute exist or not. setattr() – This function is used to set an attribute.

Làm cách nào để nhận được một danh sách các thuộc tính trong Python?

Để 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 () ..
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 ..

__ getattr __ là gì?

__getAttr __ (tự, tên) là một phương thức đối tượng được gọi là nếu không tìm thấy thuộc tính của đối tượng.Phương thức này sẽ trả về giá trị thuộc tính hoặc ném thuộc tính.Lưu ý rằng nếu thuộc tính đối tượng có thể được tìm thấy thông qua cơ chế bình thường, nó sẽ không được gọi.an object method that is called if the object's properties are not found. This method should return the property value or throw AttributeError . Note that if the object property can be found through the normal mechanism, it will not be called.

Làm thế nào để bạn truy cập các biến lớp bên ngoài lớp trong Python?

Các biến được xác định bên ngoài lớp có thể được truy cập bởi bất kỳ lớp hoặc bất kỳ phương thức nào trong lớp bằng cách chỉ viết tên biến.by just writing the variable name.