[英]check if two objects have the same attribute in python
我有以下内容:
class User:
"""A user class"""
def __init__(self, username, father)
"""Initialize the user class"""
self.username = name
self.father = father
def main():
user1 = User("foo", "oldman")
user2 = User("bar", "oldman1")
假设我有更多的人要填写他们的数据,他们将对他们父亲的名字有各种不同的输入。 如何制作 function 来检查两个或多个对象在 python 中是否具有相同的父亲属性?
尝试这个:
class User:
"""A user class"""
def __init__(self, name, father):
"""Initialize the user class"""
self.username = name
self.father = father
def check_father(self,user):
if(self.father==user.father):
return True
return False
def main():
user1 = User("foo", "oldman")
user2 = User("bar", "oldman1")
print(user2.check_father(user1))
if __name__ == "__main__":
main()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.