简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM