题解 | #修改属性2#
修改属性2
https://www.nowcoder.com/practice/7831806be5684ad6ac0f8b43e3e3558e?tpId=314&tqId=10059984&ru=/exam/oj&qru=/ta/python/question-ranking&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26tab%3DPython%25E7%25AF%2587%26topicId%3D314
import sys class Employee: def __init__(self, name, salary): self.name = name self.salary = salary def printclass(self): try: print( "{}'salary is {}, and his age is {}".format( self.name, self.salary, self.ages ) ) except: print("Error! No age") name = input() salary = int(input()) age = input() e = Employee(name, salary) print(hasattr(e, age)) #hasattr函数要学会使用 if hasattr(e, age): print(e.printclass()) #setattr函数要学会使用 else: setattr(e, "ages", age) e.printclass()