在python中检查两个对象是否具有相同的属性
我有以下资料:在python中检查两个对象是否具有相同的属性,python,python-3.x,Python,Python 3.x,我有以下资料: class User: """A user class""" def __init__(self, username, father) """Initialize the user class""" self.username = name self.father = father def ma
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")
假设我有更多的人来填写他们的数据,他们会对他们父亲的名字有各种不同的输入。在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()
就像在
user1.father==user2.father
中一样?有时是的..哪些时候不是?谢谢。马莱哈。很乐意帮忙:)