1

I am trying to find a way to get a list of all instances of the self.average variable from the Basketball class. I will then do max(list) to get the maximum value.

class Basketball:
    def __init__(self, name, goals, shots):
        self.name = name
        self.goals = goals
        self.shots = shots
        self.average = shots / goals

    def display_average(self):
        """
        >>> b1 = Basketball("Karen", 20, 80)
        >>> b1.display_average()
        'Player: Karen, average goals per game: 4'
        """
        returnString = f"Player: {self.name}, average goals per game: {self.average:.2f}"
        return returnString

if __name__ == "__main__":
    player1 = Basketball("Karen", 20, 80)
    print(player1.display_average())
    player2 = Basketball("Alex", 4, 19)
    print(player2.display_average())
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Alex Stiles
  • 151
  • 1
  • 13
  • 2
    "I am trying to find a way to get a list of all instances of the "self.average" variable from the Basketball class." It is unclear to me what you mean. You need to keep track of the instances you create of a class yourself. Classes do not keep track of their instances automatically. – juanpa.arrivillaga Feb 04 '19 at 23:39
  • I want to have a list containing all the class instances version of self.average. So it would look like (i for instance) [i1.average, i2.average, i3.average] but I created automatically when new instances are added? possibly as a function of the class? – Alex Stiles Feb 04 '19 at 23:41
  • 1
    You can remember all the `average` values in the constructor... but then again, what do you want to happen if someone changes them, e.g. if you write `player1.average=11` after it was instantiated? – zvone Feb 04 '19 at 23:43
  • 2
    Reopening because having a class autotrack its instances is a terrible way to solve this problem (and a terrible way to solve almost any problem for which such autotracking is proposed as a solution). It is almost always better to use explicit container objects. – user2357112 Feb 04 '19 at 23:50
  • 1
    Karen and Alex are `Basketball`s? `;¬)` – martineau Feb 04 '19 at 23:56
  • 2
    Why don't you store the instances of the class in a list when you create them? Then you can get the max using something like `max([i.average for i in instance_list])` – Yitzchak Blank Feb 05 '19 at 00:06
  • @AlexStiles why don't you just explicitly add your objects to a list? As pointed out, having a class automatically track it's instances is not usually the best way to do things. – juanpa.arrivillaga Feb 05 '19 at 01:07
  • @YitzchakBlank would I create the list outside of the class and just append to it? – Alex Stiles Feb 05 '19 at 03:41
  • @AlexStiles yes – Yitzchak Blank Feb 05 '19 at 04:11

1 Answers1

2

You can create a list and store all the instances of the class in it. Then, you can pull all the average values from the list and get the max from them. I would use a list comprehension like this:

if __name__ == "__main__":
    basketball_list = []
    basketball_list.append(Basketball("Karen", 20, 80))
    basketball_list.append(Basketball("Alex", 4, 19))
    best_avg = max([i.average for i in basketball_list])
Yitzchak Blank
  • 328
  • 3
  • 18