Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

ฟังก์ชัน setattr() ใน Python ทำอะไร

setattr() Method

ตั้งค่าของ Named Attribute ของ Object

Syntax

setattr(object, name, value)

Parameters: Object, Attribute Name, Value

Returns: None

ตัวอย่าง

class Male:
    name = 'Abel'

x = Male()
print('Before modification:', x.name)
setattr(x, 'name', 'Jason')
print('After modification:', x.name)

ผลลัพธ์

Before modification: Abel
After modification: Jason