题解 | #修改属性2#
修改属性2
https://www.nowcoder.com/practice/7831806be5684ad6ac0f8b43e3e3558e
name = input() salary = int(input()) age = int(input()) class Employee(object): def __init__(self, name, salary): self.name = name self.salary = salary def printclass(self): print(f"{self.name}'salary is {self.salary}, and his age is {self.age}") e = Employee(name, salary) print(hasattr(e, "age")) if hasattr(e, "age"): e.printclass() else: setattr(e, "age", age) e.printclass()
Python中的hasattr()函数和setattr()函数:
#Python学旅#