Hướng dẫn getattr() function python - hàm getattr () python

Trong hướng dẫn này, chúng tôi sẽ tìm hiểu về phương thức Python getAttr () với sự trợ giúp của các ví dụ.

Show

Phương thức

getattr(object, name[, default])
9 trả về giá trị của thuộc tính được đặt tên của một đối tượng. Nếu không tìm thấy, nó trả về giá trị mặc định được cung cấp cho hàm.

Thí dụ

class Student:
  marks = 88
  name = 'Sheeran'

person = Student()

name = getattr(person, 'name')

print(name)

marks = getattr(person, 'marks')

print(marks) # Output: Sheeran # 88


Cú pháp GetAttr ()

Cú pháp của phương pháp

getattr(object, name[, default])
9 là:

getattr(object, name[, default])

Cú pháp trên tương đương với:

object.name

tham số getAttr ()

Phương thức

getattr(object, name[, default])
9 mất nhiều tham số:

  • Đối tượng - đối tượng có giá trị của thuộc tính được đặt tên - object whose named attribute's value is to be returned
  • Tên - Chuỗi chứa tên của thuộc tính - string that contains the attribute's name
  • mặc định (tùy chọn) - giá trị được trả về khi không tìm thấy thuộc tính được đặt tên - value that is returned when the named attribute is not found

getAttr () giá trị trả về

Phương thức

getattr(object, name[, default])
9 Trả về:

  • Giá trị của thuộc tính được đặt tên của đối tượng đã cho
  • object.name
    3, nếu không tìm thấy thuộc tính có tên
  • object.name
    4 Ngoại lệ, nếu không tìm thấy thuộc tính được đặt tên và
    object.name
    3 không được xác định

Ví dụ 1: Làm thế nào GetAttr () hoạt động trong Python?

class Person:
    age = 23
    name = "Adam"

person = Person()

print('The age is:', getattr(person, "age"))

print('The age is:', person.age)

Đầu ra

The age is: 23
The age is: 23

Ví dụ 2: GetAttr () khi không tìm thấy thuộc tính được đặt tên

class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

Đầu ra

The sex is: Male
AttributeError: 'Person' object has no attribute 'sex'

Ví dụ 2: GetAttr () khi không tìm thấy thuộc tính được đặt tên

Các thuộc tính được đặt tên tình dục không có trong người lớp. Vì vậy, khi gọi phương thức

getattr(object, name[, default])
9 với giá trị mặc định
object.name
7, nó sẽ trả về nam.

class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

0
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

1
getattr(object, name[, default])
27
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

3
getattr(object, name[, default])
29____130
getattr(object, name[, default])
31
getattr(object, name[, default])
32

getattr(object, name[, default])
33
The age is: 23
The age is: 23
1
getattr(object, name[, default])
16

  • class Person:
        age = 23
        name = "Adam"
    
    person = Person()
    
    # when default value is provided
    

    print('The sex is:', getattr(person, 'sex', 'Male'))

    # when no default value is provided

    print('The sex is:', getattr(person, 'sex'))

    0
    class Person:
        age = 23
        name = "Adam"
    
    person = Person()
    
    # when default value is provided
    

    print('The sex is:', getattr(person, 'sex', 'Male'))

    # when no default value is provided

    print('The sex is:', getattr(person, 'sex'))

    1
    class Person:
        age = 23
        name = "Adam"
    
    person = Person()
    
    # when default value is provided
    

    print('The sex is:', getattr(person, 'sex', 'Male'))

    # when no default value is provided

    print('The sex is:', getattr(person, 'sex'))

    2
    class Person:
        age = 23
        name = "Adam"
    
    person = Person()
    
    # when default value is provided
    

    print('The sex is:', getattr(person, 'sex', 'Male'))

    # when no default value is provided

    print('The sex is:', getattr(person, 'sex'))

    3
    getattr(object, name[, default])
    40
  • class Person:
        age = 23
        name = "Adam"
    
    person = Person()
    
    # when default value is provided
    

    print('The sex is:', getattr(person, 'sex', 'Male'))

    # when no default value is provided

    print('The sex is:', getattr(person, 'sex'))

    0
    class Person:
        age = 23
        name = "Adam"
    
    person = Person()
    
    # when default value is provided
    

    print('The sex is:', getattr(person, 'sex', 'Male'))

    # when no default value is provided

    print('The sex is:', getattr(person, 'sex'))

    1
    getattr(object, name[, default])
    43
    class Person:
        age = 23
        name = "Adam"
    
    person = Person()
    
    # when default value is provided
    

    print('The sex is:', getattr(person, 'sex', 'Male'))

    # when no default value is provided

    print('The sex is:', getattr(person, 'sex'))

    3
    getattr(object, name[, default])
    29____130
    getattr(object, name[, default])
    31
    getattr(object, name[, default])
    48
  • Ví dụ 2: GetAttr () khi không tìm thấy thuộc tính được đặt tên
  • & NBSP; Ví dụ 3: Phân tích hiệu suất và Python GetAttr với tham số
  • getattr(object, name[, default])
    33
    The age is: 23
    The age is: 23
    1
    getattr(object, name[, default])
    16
  • class Person:
        age = 23
        name = "Adam"
    
    person = Person()
    
    # when default value is provided
    

    print('The sex is:', getattr(person, 'sex', 'Male'))

    # when no default value is provided

    print('The sex is:', getattr(person, 'sex'))

    0
    class Person:
        age = 23
        name = "Adam"
    
    person = Person()
    
    # when default value is provided
    

    print('The sex is:', getattr(person, 'sex', 'Male'))

    # when no default value is provided

    print('The sex is:', getattr(person, 'sex'))

    1
    if __name__=='__main__':
        p = Person()
        print(rgetattr(p, 'pet.favorite.color', 'calico'))
        # 'calico'
    
        try:
            # Without a default argument, `rgetattr`, like `getattr`, raises
            # AttributeError when the dotted attribute is missing
            print(rgetattr(p, 'pet.favorite.color'))
        except AttributeError as err:
            print(err)
            # 'Pet' object has no attribute 'favorite'
    
        rsetattr(p, 'pet.name', 'Sparky')
        rsetattr(p, 'residence.type', 'Apartment')
        print(p.__dict__)
        print(p.pet.name)
        # Sparky
        print(p.residence.type)
        # Apartment
    
    3
    class Person:
        age = 23
        name = "Adam"
    
    person = Person()
    
    # when default value is provided
    

    print('The sex is:', getattr(person, 'sex', 'Male'))

    # when no default value is provided

    print('The sex is:', getattr(person, 'sex'))

    3
    class Person:
        age = 23
        name = "Adam"
    
    person = Person()
    

    print('The age is:', getattr(person, "age"))

    print('The age is:', person.age)
    2
    class Person:
        age = 23
        name = "Adam"
    
    person = Person()
    
    # when default value is provided
    

    print('The sex is:', getattr(person, 'sex', 'Male'))

    # when no default value is provided

    print('The sex is:', getattr(person, 'sex'))

    5
    if __name__=='__main__':
        p = Person()
        print(rgetattr(p, 'pet.favorite.color', 'calico'))
        # 'calico'
    
        try:
            # Without a default argument, `rgetattr`, like `getattr`, raises
            # AttributeError when the dotted attribute is missing
            print(rgetattr(p, 'pet.favorite.color'))
        except AttributeError as err:
            print(err)
            # 'Pet' object has no attribute 'favorite'
    
        rsetattr(p, 'pet.name', 'Sparky')
        rsetattr(p, 'residence.type', 'Apartment')
        print(p.__dict__)
        print(p.pet.name)
        # Sparky
        print(p.residence.type)
        # Apartment
    
    7
    class Person:
        age = 23
        name = "Adam"
    
    person = Person()
    
    # when default value is provided
    

    print('The sex is:', getattr(person, 'sex', 'Male'))

    # when no default value is provided

    print('The sex is:', getattr(person, 'sex'))

    7

import functools

def rsetattr(obj, attr, val):
    pre, _, post = attr.rpartition('.')
    return setattr(rgetattr(obj, pre) if pre else obj, post, val)

# using wonder's beautiful simplification: https://stackoverflow.com/questions/31174295/getattr-and-setattr-on-nested-objects/31174427?noredirect=1#comment86638618_31174427

def rgetattr(obj, attr, *args):
    def _getattr(obj, attr):
        return getattr(obj, attr, *args)
    return functools.reduce(_getattr, [obj] + attr.split('.'))

& NBSP; Ví dụ 3: Phân tích hiệu suất và Python GetAttr với tham số


import functools

class Person(object):
    def __init__(self):
        self.pet = Pet()
        self.residence = Residence()

class Pet(object):
    def __init__(self,name='Fido',species='Dog'):
        self.name = name
        self.species = species

class Residence(object):
    def __init__(self,type='House',sqft=None):
        self.type = type
        self.sqft=sqft

def rsetattr(obj, attr, val):
    pre, _, post = attr.rpartition('.')
    return setattr(rgetattr(obj, pre) if pre else obj, post, val)

def rgetattr(obj, attr, *args):
    def _getattr(obj, attr):
        return getattr(obj, attr, *args)
    return functools.reduce(_getattr, [obj] + attr.split('.'))

if __name__=='__main__':
    p = Person()
    print(rgetattr(p, 'pet.favorite.color', 'calico'))
    # 'calico'

    try:
        # Without a default argument, `rgetattr`, like `getattr`, raises
        # AttributeError when the dotted attribute is missing
        print(rgetattr(p, 'pet.favorite.color'))
    except AttributeError as err:
        print(err)
        # 'Pet' object has no attribute 'favorite'

    rsetattr(p, 'pet.name', 'Sparky')
    rsetattr(p, 'residence.type', 'Apartment')
    print(p.__dict__)
    print(p.pet.name)
    # Sparky
    print(p.residence.type)
    # Apartment

if __name__=='__main__':
    p = Person()
    print(rgetattr(p, 'pet.favorite.color', 'calico'))
    # 'calico'

    try:
        # Without a default argument, `rgetattr`, like `getattr`, raises
        # AttributeError when the dotted attribute is missing
        print(rgetattr(p, 'pet.favorite.color'))
    except AttributeError as err:
        print(err)
        # 'Pet' object has no attribute 'favorite'

    rsetattr(p, 'pet.name', 'Sparky')
    rsetattr(p, 'residence.type', 'Apartment')
    print(p.__dict__)
    print(p.pet.name)
    # Sparky
    print(p.residence.type)
    # Apartment
9
getattr(object, name[, default])
00

getattr(object, name[, default])
33
The age is: 23
The age is: 23
1
getattr(object, name[, default])
16

  • class Person:
        age = 23
        name = "Adam"
    
    person = Person()
    
    # when default value is provided
    

    print('The sex is:', getattr(person, 'sex', 'Male'))

    # when no default value is provided

    print('The sex is:', getattr(person, 'sex'))

    0
    class Person:
        age = 23
        name = "Adam"
    
    person = Person()
    
    # when default value is provided
    

    print('The sex is:', getattr(person, 'sex', 'Male'))

    # when no default value is provided

    print('The sex is:', getattr(person, 'sex'))

    1
    class Person:
        age = 23
        name = "Adam"
    
    person = Person()
    
    # when default value is provided
    

    print('The sex is:', getattr(person, 'sex', 'Male'))

    # when no default value is provided

    print('The sex is:', getattr(person, 'sex'))

    2
    class Person:
        age = 23
        name = "Adam"
    
    person = Person()
    
    # when default value is provided
    

    print('The sex is:', getattr(person, 'sex', 'Male'))

    # when no default value is provided

    print('The sex is:', getattr(person, 'sex'))

    3
    getattr(object, name[, default])
    40
  • class Person:
        age = 23
        name = "Adam"
    
    person = Person()
    
    # when default value is provided
    

    print('The sex is:', getattr(person, 'sex', 'Male'))

    # when no default value is provided

    print('The sex is:', getattr(person, 'sex'))

    0
    class Person:
        age = 23
        name = "Adam"
    
    person = Person()
    
    # when default value is provided
    

    print('The sex is:', getattr(person, 'sex', 'Male'))

    # when no default value is provided

    print('The sex is:', getattr(person, 'sex'))

    1
    getattr(object, name[, default])
    43
    class Person:
        age = 23
        name = "Adam"
    
    person = Person()
    
    # when default value is provided
    

    print('The sex is:', getattr(person, 'sex', 'Male'))

    # when no default value is provided

    print('The sex is:', getattr(person, 'sex'))

    3
    getattr(object, name[, default])
    29____130
    getattr(object, name[, default])
    31
    getattr(object, name[, default])
    48
  • Ví dụ 2: GetAttr () khi không tìm thấy thuộc tính được đặt tên
  • & NBSP; Ví dụ 3: Phân tích hiệu suất và Python GetAttr với tham số
  • getattr(object, name[, default])
    33
    The age is: 23
    The age is: 23
    1
    getattr(object, name[, default])
    16
  • class Person:
        age = 23
        name = "Adam"
    
    person = Person()
    
    # when default value is provided
    

    print('The sex is:', getattr(person, 'sex', 'Male'))

    # when no default value is provided

    print('The sex is:', getattr(person, 'sex'))

    0
    class Person:
        age = 23
        name = "Adam"
    
    person = Person()
    
    # when default value is provided
    

    print('The sex is:', getattr(person, 'sex', 'Male'))

    # when no default value is provided

    print('The sex is:', getattr(person, 'sex'))

    1
    if __name__=='__main__':
        p = Person()
        print(rgetattr(p, 'pet.favorite.color', 'calico'))
        # 'calico'
    
        try:
            # Without a default argument, `rgetattr`, like `getattr`, raises
            # AttributeError when the dotted attribute is missing
            print(rgetattr(p, 'pet.favorite.color'))
        except AttributeError as err:
            print(err)
            # 'Pet' object has no attribute 'favorite'
    
        rsetattr(p, 'pet.name', 'Sparky')
        rsetattr(p, 'residence.type', 'Apartment')
        print(p.__dict__)
        print(p.pet.name)
        # Sparky
        print(p.residence.type)
        # Apartment
    
    3
    class Person:
        age = 23
        name = "Adam"
    
    person = Person()
    
    # when default value is provided
    

    print('The sex is:', getattr(person, 'sex', 'Male'))

    # when no default value is provided

    print('The sex is:', getattr(person, 'sex'))

    3
    class Person:
        age = 23
        name = "Adam"
    
    person = Person()
    

    print('The age is:', getattr(person, "age"))

    print('The age is:', person.age)
    2
    class Person:
        age = 23
        name = "Adam"
    
    person = Person()
    
    # when default value is provided
    

    print('The sex is:', getattr(person, 'sex', 'Male'))

    # when no default value is provided

    print('The sex is:', getattr(person, 'sex'))

    5
    if __name__=='__main__':
        p = Person()
        print(rgetattr(p, 'pet.favorite.color', 'calico'))
        # 'calico'
    
        try:
            # Without a default argument, `rgetattr`, like `getattr`, raises
            # AttributeError when the dotted attribute is missing
            print(rgetattr(p, 'pet.favorite.color'))
        except AttributeError as err:
            print(err)
            # 'Pet' object has no attribute 'favorite'
    
        rsetattr(p, 'pet.name', 'Sparky')
        rsetattr(p, 'residence.type', 'Apartment')
        print(p.__dict__)
        print(p.pet.name)
        # Sparky
        print(p.residence.type)
        # Apartment
    
    7
    class Person:
        age = 23
        name = "Adam"
    
    person = Person()
    
    # when default value is provided
    

    print('The sex is:', getattr(person, 'sex', 'Male'))

    # when no default value is provided

    print('The sex is:', getattr(person, 'sex'))

    7
getattr(object, name[, default])
0

& NBSP; Ví dụ 3: Phân tích hiệu suất và Python GetAttr với tham số

getattr(object, name[, default])
1

if __name__=='__main__':
    p = Person()
    print(rgetattr(p, 'pet.favorite.color', 'calico'))
    # 'calico'

    try:
        # Without a default argument, `rgetattr`, like `getattr`, raises
        # AttributeError when the dotted attribute is missing
        print(rgetattr(p, 'pet.favorite.color'))
    except AttributeError as err:
        print(err)
        # 'Pet' object has no attribute 'favorite'

    rsetattr(p, 'pet.name', 'Sparky')
    rsetattr(p, 'residence.type', 'Apartment')
    print(p.__dict__)
    print(p.pet.name)
    # Sparky
    print(p.residence.type)
    # Apartment
9
getattr(object, name[, default])
00

getattr(object, name[, default])
14
The age is: 23
The age is: 23
1
getattr(object, name[, default])
16

getattr(object, name[, default])
2

class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

0
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

1
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

2
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

3
class Person:
    age = 23
    name = "Adam"

person = Person()

print('The age is:', getattr(person, "age"))

print('The age is:', person.age)
2
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

5
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

6
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

7

class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

0
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

1
getattr(object, name[, default])
27
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

3
getattr(object, name[, default])
29____130
getattr(object, name[, default])
31
getattr(object, name[, default])
32

  • getattr(object, name[, default])
    33
    The age is: 23
    The age is: 23
    1
    getattr(object, name[, default])
    16
  • class Person:
        age = 23
        name = "Adam"
    
    person = Person()
    
    # when default value is provided
    

    print('The sex is:', getattr(person, 'sex', 'Male'))

    # when no default value is provided

    print('The sex is:', getattr(person, 'sex'))

    0
    class Person:
        age = 23
        name = "Adam"
    
    person = Person()
    
    # when default value is provided
    

    print('The sex is:', getattr(person, 'sex', 'Male'))

    # when no default value is provided

    print('The sex is:', getattr(person, 'sex'))

    1
    class Person:
        age = 23
        name = "Adam"
    
    person = Person()
    
    # when default value is provided
    

    print('The sex is:', getattr(person, 'sex', 'Male'))

    # when no default value is provided

    print('The sex is:', getattr(person, 'sex'))

    2
    class Person:
        age = 23
        name = "Adam"
    
    person = Person()
    
    # when default value is provided
    

    print('The sex is:', getattr(person, 'sex', 'Male'))

    # when no default value is provided

    print('The sex is:', getattr(person, 'sex'))

    3
    getattr(object, name[, default])
    40

class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

0
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

1
getattr(object, name[, default])
43
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

3
getattr(object, name[, default])
29____130
getattr(object, name[, default])
31
getattr(object, name[, default])
48
function is used to access the attribute value of an object and also gives an option of executing the default value in case of unavailability of the key.

Ví dụ 4: & nbsp; giá trị mặc định của GetAttr Python getattr(obj, key, def)

Ví dụ 5: Chức năng Python getAttr ()

  • class Person:
        age = 23
        name = "Adam"
    
    person = Person()
    

    print('The age is:', getattr(person, "age"))

    print('The age is:', person.age)
    9
    getattr(object, name[, default])
    73
    getattr(object, name[, default])
    74
    getattr(object, name[, default])
    75
    getattr(object, name[, default])
    76
    The object whose attributes need to be processed.
  • getattr(object, name[, default])
    77
    getattr(object, name[, default])
    75
    getattr(object, name[, default])
    79
    The age is: 23
    The age is: 23
    1
    getattr(object, name[, default])
    81
    The attribute of object
  • getattr(object, name[, default])
    77
    getattr(object, name[, default])
    75
    getattr(object, name[, default])
    84
    The age is: 23
    The age is: 23
    1
    getattr(object, name[, default])
    86
    The default value that need to be printed in case attribute is not found.

class Person:
    age = 23
    name = "Adam"

person = Person()

print('The age is:', getattr(person, "age"))

print('The age is:', person.age)
9
getattr(object, name[, default])
73
getattr(object, name[, default])
89
getattr(object, name[, default])
75
getattr(object, name[, default])
91
Object value if value is available, default value in case attribute is not present 
and returns AttributeError in case attribute is not present and default value is not 
specified. 

Cách thức hoạt động của GetAttr ()

Ví dụ 1: Thể hiện hoạt động của getAttr () & nbsp;

Python3

class Person:
    age = 23
    name = "Adam"

person = Person()

print('The age is:', getattr(person, "age"))

print('The age is:', person.age)
7
class Person:
    age = 23
    name = "Adam"

person = Person()

print('The age is:', getattr(person, "age"))

print('The age is:', person.age)
8

class Person:
    age = 23
    name = "Adam"

person = Person()

print('The age is:', getattr(person, "age"))

print('The age is:', person.age)
9
The age is: 23
The age is: 23
0
The age is: 23
The age is: 23
1
The age is: 23
The age is: 23
2

class Person:
    age = 23
    name = "Adam"

person = Person()

print('The age is:', getattr(person, "age"))

print('The age is:', person.age)
9
The age is: 23
The age is: 23
4
The age is: 23
The age is: 23
1
The age is: 23
The age is: 23
6

The age is: 23
The age is: 23
7
The age is: 23
The age is: 23
1
The age is: 23
The age is: 23
9

class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

0
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

1
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

2
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

3
class Person:
    age = 23
    name = "Adam"

person = Person()

print('The age is:', getattr(person, "age"))

print('The age is:', person.age)
2
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

5
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

6
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

7

class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

0
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

1
The sex is: Male
AttributeError: 'Person' object has no attribute 'sex'
0
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

3
class Person:
    age = 23
    name = "Adam"

person = Person()

print('The age is:', getattr(person, "age"))

print('The age is:', person.age)
2
The sex is: Male
AttributeError: 'Person' object has no attribute 'sex'
3

The sex is: Male
AttributeError: 'Person' object has no attribute 'sex'
4
The sex is: Male
AttributeError: 'Person' object has no attribute 'sex'
5
The sex is: Male
AttributeError: 'Person' object has no attribute 'sex'
6

The sex is: Male
AttributeError: 'Person' object has no attribute 'sex'
4
The sex is: Male
AttributeError: 'Person' object has no attribute 'sex'
8
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

7

class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

0
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

1
import functools

def rsetattr(obj, attr, val):
    pre, _, post = attr.rpartition('.')
    return setattr(rgetattr(obj, pre) if pre else obj, post, val)

# using wonder's beautiful simplification: https://stackoverflow.com/questions/31174295/getattr-and-setattr-on-nested-objects/31174427?noredirect=1#comment86638618_31174427

def rgetattr(obj, attr, *args):
    def _getattr(obj, attr):
        return getattr(obj, attr, *args)
    return functools.reduce(_getattr, [obj] + attr.split('.'))
2
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

3
class Person:
    age = 23
    name = "Adam"

person = Person()

print('The age is:', getattr(person, "age"))

print('The age is:', person.age)
2
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

5
import functools

def rsetattr(obj, attr, val):
    pre, _, post = attr.rpartition('.')
    return setattr(rgetattr(obj, pre) if pre else obj, post, val)

# using wonder's beautiful simplification: https://stackoverflow.com/questions/31174295/getattr-and-setattr-on-nested-objects/31174427?noredirect=1#comment86638618_31174427

def rgetattr(obj, attr, *args):
    def _getattr(obj, attr):
        return getattr(obj, attr, *args)
    return functools.reduce(_getattr, [obj] + attr.split('.'))
6
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

7

Output:  

getattr(object, name[, default])
3

Exception: 

getattr(object, name[, default])
4

Ví dụ 2: GetAttr () khi không tìm thấy thuộc tính được đặt tên

Python3

class Person:
    age = 23
    name = "Adam"

person = Person()

print('The age is:', getattr(person, "age"))

print('The age is:', person.age)
7
class Person:
    age = 23
    name = "Adam"

person = Person()

print('The age is:', getattr(person, "age"))

print('The age is:', person.age)
8

class Person:
    age = 23
    name = "Adam"

person = Person()

print('The age is:', getattr(person, "age"))

print('The age is:', person.age)
9
The age is: 23
The age is: 23
0
The age is: 23
The age is: 23
1
The age is: 23
The age is: 23
2

class Person:
    age = 23
    name = "Adam"

person = Person()

print('The age is:', getattr(person, "age"))

print('The age is:', person.age)
9
The age is: 23
The age is: 23
4
The age is: 23
The age is: 23
1
The age is: 23
The age is: 23
6

The age is: 23
The age is: 23
7
The age is: 23
The age is: 23
1
The age is: 23
The age is: 23
9

class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

0
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

1
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

2
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

3
class Person:
    age = 23
    name = "Adam"

person = Person()

print('The age is:', getattr(person, "age"))

print('The age is:', person.age)
2
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

5
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

6
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

7

Output:

getattr(object, name[, default])
5

class Person: age = 23 name = "Adam" person = Person() # when default value is provided print('The sex is:', getattr(person, 'sex', 'Male')) # when no default value is provided print('The sex is:', getattr(person, 'sex'))0class Person: age = 23 name = "Adam" person = Person() # when default value is provided print('The sex is:', getattr(person, 'sex', 'Male')) # when no default value is provided print('The sex is:', getattr(person, 'sex'))1The sex is: Male AttributeError: 'Person' object has no attribute 'sex'0 class Person: age = 23 name = "Adam" person = Person() # when default value is provided print('The sex is:', getattr(person, 'sex', 'Male')) # when no default value is provided print('The sex is:', getattr(person, 'sex'))3 class Person: age = 23 name = "Adam" person = Person() print('The age is:', getattr(person, "age")) print('The age is:', person.age)2The sex is: Male AttributeError: 'Person' object has no attribute 'sex'3

Python3

class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

0
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

1
import functools

def rsetattr(obj, attr, val):
    pre, _, post = attr.rpartition('.')
    return setattr(rgetattr(obj, pre) if pre else obj, post, val)

# using wonder's beautiful simplification: https://stackoverflow.com/questions/31174295/getattr-and-setattr-on-nested-objects/31174427?noredirect=1#comment86638618_31174427

def rgetattr(obj, attr, *args):
    def _getattr(obj, attr):
        return getattr(obj, attr, *args)
    return functools.reduce(_getattr, [obj] + attr.split('.'))
2
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

3
class Person:
    age = 23
    name = "Adam"

person = Person()

print('The age is:', getattr(person, "age"))

print('The age is:', person.age)
2
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

5
import functools

def rsetattr(obj, attr, val):
    pre, _, post = attr.rpartition('.')
    return setattr(rgetattr(obj, pre) if pre else obj, post, val)

# using wonder's beautiful simplification: https://stackoverflow.com/questions/31174295/getattr-and-setattr-on-nested-objects/31174427?noredirect=1#comment86638618_31174427

def rgetattr(obj, attr, *args):
    def _getattr(obj, attr):
        return getattr(obj, attr, *args)
    return functools.reduce(_getattr, [obj] + attr.split('.'))
6
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

7

class Person:
    age = 23
    name = "Adam"

person = Person()

print('The age is:', getattr(person, "age"))

print('The age is:', person.age)
7
class Person:
    age = 23
    name = "Adam"

person = Person()

print('The age is:', getattr(person, "age"))

print('The age is:', person.age)
8

class Person:
    age = 23
    name = "Adam"

person = Person()

print('The age is:', getattr(person, "age"))

print('The age is:', person.age)
9
The age is: 23
The age is: 23
0
The age is: 23
The age is: 23
1
The age is: 23
The age is: 23
2

class Person:
    age = 23
    name = "Adam"

person = Person()

print('The age is:', getattr(person, "age"))

print('The age is:', person.age)
9
The age is: 23
The age is: 23
4
The age is: 23
The age is: 23
1
The age is: 23
The age is: 23
6

The age is: 23
The age is: 23
7
The age is: 23
The age is: 23
1
The age is: 23
The age is: 23
9

class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

0
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

1
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

2
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

3
class Person:
    age = 23
    name = "Adam"

person = Person()

print('The age is:', getattr(person, "age"))

print('The age is:', person.age)
2
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

5
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

6
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

7

class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

0
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

1
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

2
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

3
class Person:
    age = 23
    name = "Adam"

person = Person()

print('The age is:', getattr(person, "age"))

print('The age is:', person.age)
2
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

5
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

6
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

7

class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

0
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

1
The sex is: Male
AttributeError: 'Person' object has no attribute 'sex'
0
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

3
class Person:
    age = 23
    name = "Adam"

person = Person()

print('The age is:', getattr(person, "age"))

print('The age is:', person.age)
2
The sex is: Male
AttributeError: 'Person' object has no attribute 'sex'
3

class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

0
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

1
import functools

def rsetattr(obj, attr, val):
    pre, _, post = attr.rpartition('.')
    return setattr(rgetattr(obj, pre) if pre else obj, post, val)

# using wonder's beautiful simplification: https://stackoverflow.com/questions/31174295/getattr-and-setattr-on-nested-objects/31174427?noredirect=1#comment86638618_31174427

def rgetattr(obj, attr, *args):
    def _getattr(obj, attr):
        return getattr(obj, attr, *args)
    return functools.reduce(_getattr, [obj] + attr.split('.'))
2
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

3
class Person:
    age = 23
    name = "Adam"

person = Person()

print('The age is:', getattr(person, "age"))

print('The age is:', person.age)
2
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

5
import functools

def rsetattr(obj, attr, val):
    pre, _, post = attr.rpartition('.')
    return setattr(rgetattr(obj, pre) if pre else obj, post, val)

# using wonder's beautiful simplification: https://stackoverflow.com/questions/31174295/getattr-and-setattr-on-nested-objects/31174427?noredirect=1#comment86638618_31174427

def rgetattr(obj, attr, *args):
    def _getattr(obj, attr):
        return getattr(obj, attr, *args)
    return functools.reduce(_getattr, [obj] + attr.split('.'))
6
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

7

Ví dụ 2: GetAttr () khi không tìm thấy thuộc tính được đặt tên

class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

0
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

1
if __name__=='__main__':
    p = Person()
    print(rgetattr(p, 'pet.favorite.color', 'calico'))
    # 'calico'

    try:
        # Without a default argument, `rgetattr`, like `getattr`, raises
        # AttributeError when the dotted attribute is missing
        print(rgetattr(p, 'pet.favorite.color'))
    except AttributeError as err:
        print(err)
        # 'Pet' object has no attribute 'favorite'

    rsetattr(p, 'pet.name', 'Sparky')
    rsetattr(p, 'residence.type', 'Apartment')
    print(p.__dict__)
    print(p.pet.name)
    # Sparky
    print(p.residence.type)
    # Apartment
3
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

3
class Person:
    age = 23
    name = "Adam"

person = Person()

print('The age is:', getattr(person, "age"))

print('The age is:', person.age)
2
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

5
if __name__=='__main__':
    p = Person()
    print(rgetattr(p, 'pet.favorite.color', 'calico'))
    # 'calico'

    try:
        # Without a default argument, `rgetattr`, like `getattr`, raises
        # AttributeError when the dotted attribute is missing
        print(rgetattr(p, 'pet.favorite.color'))
    except AttributeError as err:
        print(err)
        # 'Pet' object has no attribute 'favorite'

    rsetattr(p, 'pet.name', 'Sparky')
    rsetattr(p, 'residence.type', 'Apartment')
    print(p.__dict__)
    print(p.pet.name)
    # Sparky
    print(p.residence.type)
    # Apartment
7
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

7

Output:  

getattr(object, name[, default])
6

& NBSP; Ví dụ 3: Phân tích hiệu suất và Python GetAttr với tham số

Python3

class Person:
    age = 23
    name = "Adam"

person = Person()

print('The age is:', getattr(person, "age"))

print('The age is:', person.age)
7
class Person:
    age = 23
    name = "Adam"

person = Person()

print('The age is:', getattr(person, "age"))

print('The age is:', person.age)
8

class Person:
    age = 23
    name = "Adam"

person = Person()

print('The age is:', getattr(person, "age"))

print('The age is:', person.age)
9
The age is: 23
The age is: 23
0
The age is: 23
The age is: 23
1
The age is: 23
The age is: 23
2

class Person:
    age = 23
    name = "Adam"

person = Person()

print('The age is:', getattr(person, "age"))

print('The age is:', person.age)
9
The age is: 23
The age is: 23
4
The age is: 23
The age is: 23
1
The age is: 23
The age is: 23
6

The age is: 23
The age is: 23
7
The age is: 23
The age is: 23
1
The age is: 23
The age is: 23
9

class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

0
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

1
import functools

def rsetattr(obj, attr, val):
    pre, _, post = attr.rpartition('.')
    return setattr(rgetattr(obj, pre) if pre else obj, post, val)

# using wonder's beautiful simplification: https://stackoverflow.com/questions/31174295/getattr-and-setattr-on-nested-objects/31174427?noredirect=1#comment86638618_31174427

def rgetattr(obj, attr, *args):
    def _getattr(obj, attr):
        return getattr(obj, attr, *args)
    return functools.reduce(_getattr, [obj] + attr.split('.'))
2
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

3
class Person:
    age = 23
    name = "Adam"

person = Person()

print('The age is:', getattr(person, "age"))

print('The age is:', person.age)
2
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

5
import functools

def rsetattr(obj, attr, val):
    pre, _, post = attr.rpartition('.')
    return setattr(rgetattr(obj, pre) if pre else obj, post, val)

# using wonder's beautiful simplification: https://stackoverflow.com/questions/31174295/getattr-and-setattr-on-nested-objects/31174427?noredirect=1#comment86638618_31174427

def rgetattr(obj, attr, *args):
    def _getattr(obj, attr):
        return getattr(obj, attr, *args)
    return functools.reduce(_getattr, [obj] + attr.split('.'))
6
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

7

Output:

getattr(object, name[, default])
7

Ví dụ 2: GetAttr () khi không tìm thấy thuộc tính được đặt tên

Python3

class Person:
    age = 23
    name = "Adam"

person = Person()

print('The age is:', getattr(person, "age"))

print('The age is:', person.age)
7
class Person:
    age = 23
    name = "Adam"

person = Person()

print('The age is:', getattr(person, "age"))

print('The age is:', person.age)
8

class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

0
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

1
if __name__=='__main__':
    p = Person()
    print(rgetattr(p, 'pet.favorite.color', 'calico'))
    # 'calico'

    try:
        # Without a default argument, `rgetattr`, like `getattr`, raises
        # AttributeError when the dotted attribute is missing
        print(rgetattr(p, 'pet.favorite.color'))
    except AttributeError as err:
        print(err)
        # 'Pet' object has no attribute 'favorite'

    rsetattr(p, 'pet.name', 'Sparky')
    rsetattr(p, 'residence.type', 'Apartment')
    print(p.__dict__)
    print(p.pet.name)
    # Sparky
    print(p.residence.type)
    # Apartment
3
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

3
class Person:
    age = 23
    name = "Adam"

person = Person()

print('The age is:', getattr(person, "age"))

print('The age is:', person.age)
2
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

5
if __name__=='__main__':
    p = Person()
    print(rgetattr(p, 'pet.favorite.color', 'calico'))
    # 'calico'

    try:
        # Without a default argument, `rgetattr`, like `getattr`, raises
        # AttributeError when the dotted attribute is missing
        print(rgetattr(p, 'pet.favorite.color'))
    except AttributeError as err:
        print(err)
        # 'Pet' object has no attribute 'favorite'

    rsetattr(p, 'pet.name', 'Sparky')
    rsetattr(p, 'residence.type', 'Apartment')
    print(p.__dict__)
    print(p.pet.name)
    # Sparky
    print(p.residence.type)
    # Apartment
7
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

7

& NBSP; Ví dụ 3: Phân tích hiệu suất và Python GetAttr với tham số

if __name__=='__main__':
    p = Person()
    print(rgetattr(p, 'pet.favorite.color', 'calico'))
    # 'calico'

    try:
        # Without a default argument, `rgetattr`, like `getattr`, raises
        # AttributeError when the dotted attribute is missing
        print(rgetattr(p, 'pet.favorite.color'))
    except AttributeError as err:
        print(err)
        # 'Pet' object has no attribute 'favorite'

    rsetattr(p, 'pet.name', 'Sparky')
    rsetattr(p, 'residence.type', 'Apartment')
    print(p.__dict__)
    print(p.pet.name)
    # Sparky
    print(p.residence.type)
    # Apartment
9
getattr(object, name[, default])
00

getattr(object, name[, default])
14
The age is: 23
The age is: 23
1
getattr(object, name[, default])
16

getattr(object, name[, default])
77
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

0
getattr(object, name[, default])
94
getattr(object, name[, default])
95
getattr(object, name[, default])
96

class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

0
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

1
getattr(object, name[, default])
27
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

3
getattr(object, name[, default])
29____130
getattr(object, name[, default])
31
getattr(object, name[, default])
32

getattr(object, name[, default])
33
The age is: 23
The age is: 23
1
getattr(object, name[, default])
16

class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

0
object.name
07

class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

0
object.name
09

class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

0
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

1
class Person:
    age = 23
    name = "Adam"

person = Person()

print('The age is:', getattr(person, "age"))

print('The age is:', person.age)
2
The sex is: Male
AttributeError: 'Person' object has no attribute 'sex'
3
object.name
14
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

7

class Person:
    age = 23
    name = "Adam"

person = Person()

print('The age is:', getattr(person, "age"))

print('The age is:', person.age)
2
The sex is: Male
AttributeError: 'Person' object has no attribute 'sex'
3
object.name
14
object.name
19
object.name
20
getattr(object, name[, default])
96

Output:

getattr(object, name[, default])
8

class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

0
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

1
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

2
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

3
getattr(object, name[, default])
40
Conventional method takes less time than getattr(), but when default values have to be used in case of missing attributes, getattr() is a good choice.

class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

0
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

1
getattr(object, name[, default])
43
class Person:
    age = 23
    name = "Adam"

person = Person()

# when default value is provided

print('The sex is:', getattr(person, 'sex', 'Male'))

# when no default value is provided

print('The sex is:', getattr(person, 'sex'))

3
getattr(object, name[, default])
29____130
getattr(object, name[, default])
31
getattr(object, name[, default])
48
The are many applications of getattr(), a few of them already mentioned in cases of absence of attributes of objects, in web developments where some of the form attributes are optional. Also useful in cases of Machine Learning feature collections in case some features sometimes go missing in data collection.