ฟังก์ชันใน Python เป็น objects จึงมี attributes เช่นเดียวกับออบเจ็กต์ทั่วไป
- Built-in attribute:
__doc__(docstring) - กำหนด attribute ใหม่ได้ด้วย
setattr()หรือ dot notation - ดึงค่าได้ด้วย
getattr()หรือ dot notation
ตัวอย่าง
def foo(): pass setattr(foo, 'age', 23) setattr(foo, 'name', 'John Doe') print(getattr(foo, 'age')) # 23 foo.gender = 'male' print(foo.gender) # male print(foo.name) # John Doe print(foo.age) # 23
setattr(obj, 'attr', value) เทียบเท่า obj.attr = value