Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中检查两个对象是否具有相同的属性_Python_Python 3.x - Fatal编程技术网

在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
中一样?有时是的..哪些时候不是?谢谢。马莱哈。很乐意帮忙:)