Dec-29-2019, 09:37 AM
You can use the
The
Dataclasses could do this automatically. Since Python 3.7 they are available.
After
the self.relative is overwritten with the calculated value.
You can also define how dataclasses are sorted.
For example you can make more then on field to compare, but you have to exclude other
fields, if they are not used for sorting. In this case relative is the only field to compare.
max
function with lambda:max(data, key=lambda vote: vote.voters / vote.population).nameInstead you could write it as a function, which is easier to unterstand.
def relative(country): return country.voters / country.population max(data, key=relative).nameJust adding the calculated percentages to the class is a better solution.
from functools import total_ordering @total_ordering class County: def __init__(self, name, population, voters): self.name = name self.population = population self.voters = voters self.relative = voters / population def __lt__(self, other): return self.relative < other.relative def __gt__(self, other): return self.relative > other.relative def __eg__(self, other): return self.relative == other.relativeNow you can apply max on the data list. The function max returns the whole object from the list.
The
total_ordering
decorator modifies the class. It will create the missing comparisons like <=
>=
and !=
.allegheny = County("allegheny", 1000490, 645469) philadelphia = County("philadelphia", 1134081, 539069) montgomery = County("montgomery", 568952, 399591) lancaster = County("lancaster", 345367, 230278) delaware = County("delaware", 414031, 284538) chester = County("chester", 319919, 230823) bucks = County("bucks", 444149, 319816) data = [allegheny, philadelphia, montgomery, lancaster, delaware, chester, bucks] result = max(data) print(result, type(result), result.name)
Output:<__main__.County object at 0x7f6f47658610> <class '__main__.County'> chester
As you can see, the representation is missing. It could be added with the method __repr__, but it's not a must.Dataclasses could do this automatically. Since Python 3.7 they are available.
from dataclasses import dataclass, field @dataclass(order=True) class County: name: str = field(compare=False) population: int = field(compare=False) voters: int = field(compare=False) relative: float = field(compare=True, default=0.0) def __post_init__(self): self.relative: float = self.voters / self.population
County('Test', 100, 1)
Output:County(name='Test', population=100, voters=1, relative=0.01)
So it has also a nice presentation. In this case the relative value is set to 0.0.After
__post_init__
was called by dataclass itself,the self.relative is overwritten with the calculated value.
You can also define how dataclasses are sorted.
For example you can make more then on field to compare, but you have to exclude other
fields, if they are not used for sorting. In this case relative is the only field to compare.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!