题解 | #修改属性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()

全部评论

相关推荐

尊尼获获:闺蜜在哪?
点赞 评论 收藏
分享
09-29 11:19
门头沟学院 Java
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务