简体   繁体   English

检查 python 中的两个对象是否具有相同的属性

[英]check if two objects have the same attribute in python

I have the following:我有以下内容:

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")

Assume I have more people to fill in their data, and they will have all kind of different inputs about their father's name.假设我有更多的人要填写他们的数据,他们将对他们父亲的名字有各种不同的输入。 How can I make a function to check if two or more objects have the same father attribute in python?如何制作 function 来检查两个或多个对象在 python 中是否具有相同的父亲属性?

Try this:尝试这个:

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.

相关问题 使用==检查python中两个对象的值是否相同 - use == to check if two objects have the same value or not in python 如何检查两个列表是否具有相同顺序的相同对象? - How to check if two lists have the same objects in the same order? 检索列表中的所有对象在python中的属性是否具有相同的值 - Retrieving if all objects in a list have the same value for an attribute in python 如何检查 2 class 对象在 python 的列表中是否具有相同的参数? - How to check if 2 class objects have same parameters inside a list in python? 检查python列表中是否有两个(或多个)具有相同属性值的自定义对象 - Check if there are two (or more) custom object with the same attribute value in a python list 在 python 中,相同 class 的两个不同对象可以有不同的变量数 - In python can two different objects of same class have different no of variables 是否可以区分 Python 中具有相同变量的两个对象? - Is it possible to differentiate between two objects that have the same variable in Python? 如何让两个对象在python中具有相同的id? - How to make two objects have the same id in python? 如何在Python中检查两个文件(字符串和文件)是否具有相同的内容? - How in Python check if two files ( String and file ) have same content? 如何检查两个python pathlib.Path是否具有相同的父项? - How to check that two python pathlib.Path have the same parents?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM