0

i'm still pretty new to python and oop, i have the solution for my problem but it's not really performant and i think i miss something.

My code :

class User1:
    
    def __init__(self, foo):
        self.foo = foo

class User2:
    
    def __init__(self, foo):
        self.foo = foo

list_of_user1 = getUser1()
list_of_user2 = getUser2()

def do_something_to_user1():

    do_something_to_user = []  

    for user in list_of_user1:
        if user.foo not in [user.foo for user in list_of_user2]:
            do_something_to_user.append(user)
    for user in do_something_to_user:
        something(user)

def do_something_to_user2():

    do_something_to_user = []  

    for user in list_of_user2:
        if user.foo not in [user.foo for user in list_of_user1]:
            do_something_to_user.append(user)
    for user in do_something_to_user:
        something_else(user)

My question is, how should i compare two object of different class for multiple instance of these class . Is there a better way to do this ?

bakelue
  • 25
  • 5
  • In OOP you usually define a single `User` class that you instantiate multiple times to give you multiple instances of the class `User`. In Python, you can then write the class method [`__eq__(self, other)`](https://docs.python.org/3/reference/datamodel.html#object.__eq__) to compare the instances. – felipe Oct 11 '22 at 12:32
  • 1
    You seem to be looking for set-like functionality, since you want all user1 not in list of user2 and *vice versa*. This could be achieved by implementing [\_\_hash\_\_](https://docs.python.org/3/reference/datamodel.html#object.__hash__) and [\_\_eq\_\_](https://docs.python.org/3/reference/datamodel.html#object.__eq__), which could be as simple as returning `hash(self.foo)` and `self.foo == other.foo` respectively. With that is place, you could then do e.g. `set(list_of_user1) - set(list_of_user2)`. – ekhumoro Oct 11 '22 at 13:39

1 Answers1

1

Following on the comment, the usual OOP way of doing things is by creating a single class User that you instantiate multiple times;

class User:
    def __init__(self, name: str, age: int):
        self.name = name
        self.age = age

user1 = User("Bob", 20)
user2 = User("Alice", 21)

To compare these two instances, you can implement the __eq__ method.

class User:
    def __init__(self, name: str, age: int):
        self.name = name
        self.age = age

    def __eq__(self, other):
        return self.name == other.name and self.age == other.age


user1 = User("Bob", 20)
user2 = User("Alice", 21)
user3 = User("Bob", 20)

print(user1 == user2) # False
print(user1 == user3) # True

In some cases in OOP you might have different types of users. When this occur, this is usually where inheritance is used;

class User:
    def __init__(self, name: str, age: int):
        self.name = name
        self.age = age

    def __eq__(self, other):
        return self.name == other.name and self.age == other.age


class Student(User):
    def __init__(self, name: str, age: int, student_id: int):
        super().__init__(name, age)
        self.student_id = student_id

    def __eq__(self, other):
        return super().__eq__(other) and self.student_id == other.student_id


class Teacher(User):
    def __init__(self, name: str, age: int, teacher_id: int):
        super().__init__(name, age)
        self.teacher_id = teacher_id

    def __eq__(self, other):
        return super().__eq__(other) and self.teacher_id == other.teacher_id

I advice looking further into super(), but in short, it calls the parents class method. Hopefully this helps you understand the relationship structure that OOP enforces a little bit better.

felipe
  • 7,324
  • 2
  • 28
  • 37