20
votes

This is the code I written so far, and the point with the program is to read 20 people from a file and then assign them their attributes, then normalise their values from a input given by the user.

class One:
    def __init__(self):
        self.attrOne = ()
        self.attrTwo = ()
        self.attrThree = ()
        self.attrFour = ()
        self.attrFive= ()
        self.attrSix = ()
        self.attrSeven = ()
        self.attrEight = ()
        self.attrNine = ()


class Two:

    def __init__(self):
        self.allPersons = []

   def importFromList(self, filename): 
       file= open(filename, "rU")
       for line in file:
           partOfList = line.split()                        
           x = Partner() 
           x.attrOne = partOfList[0]
           x.attrTwo = partOfList[1]
           x.attrThree = partOfList[2]
           x.attrFour = partOfList[3]
           x.attrFive = partOfList[4]
           x.attrSix = partOfList[5]
           x.attrSeven = partOfList[6]
           x.attrEight= partOfList[7]
           x.attrNine = partOfList[8]
           self.addPerson(x)
        file.close()

def addPerson(self, x):
    self.allPersons.append(x) 

What I wonder is how to loop through the attributes of the persons that is placed in allPersons list and then compare them against eachother to find out the max value. This is what I tried so far, but I can't get it to work

def getMaxValue(self): 
    o = One()
    for eachPartner in self.allPartners:
        maxValuesAttrOne = max(O.attrOne))

All help will be appreciated, and I'm open for new solutions, also I imagine the importFromList method is not the most effective one, so if you got any objections I'm willing to listen and learn!

1
Is there a reason you have 9 separate attributes named attrOne through attrNine instead of, say, a single attribute which is a list of 9 values, or a dict mapping 9 names to values? - abarnert
Also, why does class One have 9 attributes all set to an empty tuple, while Partner has 9 attributes with the same names each set to a string? That seems like a recipe for confusion… - abarnert
The attributes are definitions of a person, for example name, age and wealth. Renamned them before posting here. Is it better to do self.name = name instead of an empty tuple? Also how do you mean by having a dict mapping 9 names to values, we didn't dig to deep on how to use dictionaries in the course I did, but would love an example! @abarnert - Filip
If you actually have a name that you want to store, certainly it's better to store self.name = name instead of storing an empty tuple and then forgetting the name! But if you don't have a name, using () as an "initializing value" for an attribute meant to hold strings is very weird. Either don't initialize it at all (so there will be no attrOne attribute until you have a real value to store there—which is perfectly fine; you're allowed to add new attributes to objects after __init__), or initialize it to '' or None. - abarnert
As for the dict idea… if these are real attributes like name and age, you probably don't want to do that. When you have a bunch of attributes with names like attrOne and attrTwo, that implies that you're going to be writing code that tries to read an attribute chosen dynamically based on some index or something, and that's almost always a bad idea. It doesn't sound like you have any intention of doing anything like that. So, don't worry about that part. - abarnert

1 Answers

42
votes

max() takes a key parameter, a function that when passed one of the objects returns the value by which to compare them.

Use operator.attrgetter() to get that value:

from operator import attrgetter

max(self.allPartners, key=attrgetter('attrOne'))

This returns the matching object for which that attribute is the maximum. If you wanted to store just that maximum value itself, you have two options:

  • Take the attribute from the returned object:

    max(self.allPartners, key=attrgetter('attrOne')).attrOne
    
  • Pass just the attributes instead to max() with a generator expression:

    max(p.attrOne for p in self.allPartners)
    

If you find that you need to order the One classes in various directions by the same attribute again and again (to find the minimum, maximum, sort them, etc.) you may want to make your class orderable as well.

To do that, you'll need to implement some of the basic customization hooks Python will look for. With some extra trickery, you can get away with just the lower-than and equals operations, and by using the funtools.total_ordering class decorator:

from functools import total_ordering

@total_ordering
class One:
    # ...
    def __lt__(self, other):
        if not isinstance(other, type(self)): return NotImplemented
        return self.attrOne < other.attrOne

    def __eq__(self, other):
        if not isinstance(other, type(self)): return NotImplemented
        return self.attrOne == other.attrOne

Now your One class is orderable, entirely on the basis of attrOne; for the max() function, that means you can drop the key parameter altogether.