题解 | #修改属性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学旅#
全部评论

相关推荐

11-15 19:28
已编辑
蚌埠坦克学院 硬件开发
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务