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

相关推荐

offer多多的六边形战士很无语:看了你的博客,感觉挺不错的,可以把你的访问量和粉丝数在简历里提一下,闪光点(仅个人意见)
点赞 评论 收藏
分享
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务