Masoumeh Javanbakht
Masoumeh Javanbakht

Reputation: 135

How to use a method in a class to compare two objects of the class

Hi guys I am totally new in oop in python. I am trying to write a class of students that gets the number of students, their ages, their heights and their weights and store these information as 3 separate lists.

I aim to compute the average of ages and weights as well as heights. I don't have any problem so far.

In the next step I want to compare the average age of two instances of the class plus the average of the weights.

As it's an exercise in oop I should do it by a method of the class. But, I don't know that is it possible to do it using a method in the original class (class School) or I should create a subclass to compare the attributes of two instances of the School class.

Any help is really appreciated.

Here is my code:

class School:

    avg_age = 0
    avg_heigt = 0
    avg_weight = 0

    def __init__(self):

        self.n =int(input())

        self.list_ages = [float(x) for x in input().split(" ")]

        self.list_higt = [float(x) for x in input().split(" ")]

        self.list_weight = [float(x) for x in input().split(" ")]      

    def get_av(self):
        School.avg_age = sum(self.list_ages) / len(self.list_ages)

        School.avg_heigt = sum(self.list_higt)/len(self.list_higt)

        Scoohl.avg_weight = sum(self.list_weight)/len(self.list_weight)
        return("{},{},{}".format(School.avg_age,School.avg_heigt,School.avg_weight))  

Upvotes: 2

Views: 1660

Answers (2)

Patrick Artner
Patrick Artner

Reputation: 51643

You mix class attributes and instance attributes. Class attributes are shared between instances:

class Banks:
    total = 0
    def __init__(self,name,money):
        self.name=name
        self.money=money
        Banks.total += money


b1 = Banks("one",100)
b2 = Banks("two",5000000)

# prints 5000100 - all the money of all banks, 
# does not help you comparing avgs ov instances at all
print(b1.total)  

Output:

5000100

You need seperated averages per instance and a function that compares one instance (self) agiant another instance:

class School: 
    def __init__(self): 
        # self.n =int(input()) # not needed - use len(one of your lists) 
        list_ages = [float(x) for x in input("Gimme ages, space seperated: ").split()] 
        list_hight = [float(x) for x in input("Gimme hights, space seperated: ").split()] 
        list_weight = [float(x) for x in input("Gimme weights, space seperated: ").split()] 

        # shortest list downsizes all other lists 
        self.list_ages, self.list_hight, self.list_weight = zip(
            *zip( list_ages,list_hight,list_weight ))

    def get_av(self):
        self.avg_age = sum(self.list_ages) / len(self.list_ages)
        print(self.avg_age)
        self.avg_height = sum(self.list_hight) / len(self.list_hight)
        print(self.avg_height)
        self.avg_weight = sum(self.list_weight) / len(self.list_weight)
        print(self.avg_weight)  
        return  self.avg_age, self.avg_height, self.avg_weight

    def compare(self,other):
        self.get_av()
        other.get_av()
        print("Our pupils are younger: ", self.avg_age < other.avg_age)
        print("Our pupils are smaller: ", self.avg_height < other.avg_height)
        print("Our pupils are lighter: ", self.avg_weight < other.avg_weight)

c = School() # 4 5 6        22 44 66 88 99       20.2 20.2 20.2 20.2 20.2 20.2
d = School() # 100          100                  100

c.compare(d)

Output (formatted with newlines in between):

Gimme ages, space seperated: 4 5 6
Gimme hights, space seperated: 22 44 66 88 99 
Gimme weights, space seperated:  20.2 20.2 20.2 20.2 20.2 20.2

Gimme ages, space seperated: 100
Gimme hights, space seperated: 100 
Gimme weights, space seperated: 100

5.0
44.0
20.2

100.0
100.0
100.0

Our pupils are younger:  True
Our pupils are smaller:  True
Our pupils are lighter:  True

More info:

Upvotes: 0

tandat
tandat

Reputation: 255

You can do it by creating an instance function with one more parameter or using static method style with staticmethod decorator

class School:
    # your code

    def compare_to(self, other)
    # compare an instancte with other instance
    # your compare code here
        pass

    @staticmethod
    def compare(first_school, second_school):
    # first and second school are two objects of class school
    # your compare code
        pass

And you can call your function in two styles

s1 = School()
s2 = School()
# Your init data
s1.compare_to(s2)
School.compare(s1, s2)

Edit: As @Patrick answer, you should declare your variables as instance variablesto make them store differently per instance

Upvotes: 1

Related Questions