在Python中,可以使用`setattr()`函数来设置对象的属性。以下是`setattr()`函数的语法和如何使用它的示例:
setattr(object, name, value)
`object`:要设置属性的对象。
`name`:要设置的属性名称。
`value`:要设置的属性值。
示例

class Person:def __init__(self, name, age):self.name = nameself.age = age创建一个Person对象person = Person("Alice", 25)使用setattr函数设置属性值setattr(person, "city", "New York")输出属性值print(person.name) Aliceprint(person.age) 25print(person.city) New York
在这个例子中,我们首先创建了一个`Person`类的实例`person`,然后使用`setattr()`函数给`person`对象添加了一个新的属性`city`,并设置了其值为`New York`。
