How to fold the feature attributes of a Python class?

There is a class based on it there are several objects with an attribute weight, how to collect attributes in a list, in order to summarize or highlight the largest or smallest value?
April 7th 20 at 15:26
2 answers
April 7th 20 at 15:28
Solution
class MyClass:
 instance_ref = []

 def __init__(self):
MyClass.instance_ref.append(self)


a = MyClass()
b = MyClass()
c = MyClass()
d = MyClass()
a.weight = 3
b.weight = 4
c.weight = 5
d.weight = 6
add = sum([x.weight for x in MyClass.instance_ref])
print(add)
maximum = max([x.weight for x in MyClass.instance_ref])
print(maximum)
Thank you, if there is an implementation using the magic methods? - Katrine commented on April 7th 20 at 15:31
@celestino_Cruickshank98, do not quite understand what you mean. You can read more? Here I am using the magic method "__init__" - Gage_Hoeger57 commented on April 7th 20 at 15:34
I mean, __iter__ or something like that maybe I do not understand - Katrine commented on April 7th 20 at 15:37
April 7th 20 at 15:30
my_list = list()
my_list.append(obj1.weight)
my_list.append(obj2.weight)
print(max(my_list))
But if objects a hundred? - Katrine commented on April 7th 20 at 15:33
@celestino_Cruickshank98, and the hundreds of objects gathered in a pattern, or just scattered through the code (don't ever do that)? If you or someone has created hundreds of variables, only handles or through the list of all variables. - adolfo_Strom commented on April 7th 20 at 15:36
@celestino_Cruickshank98, you have a hundred stored somewhere? you can get everything through locals/globals, and check the type, but it is a very bad decision and talking about the problem in the architecture - lois.Hettinger commented on April 7th 20 at 15:39
@flossie71, tell me what objects to collect in the structure - Katrine commented on April 7th 20 at 15:42
@celestino_Cruickshank98, the most obvious data structure for objects of a custom class list:
my_list = list()
my_list.append(obg1)
my_list.append(obg2)
...
- adolfo_Strom commented on April 7th 20 at 15:45
@flossie71, and a generator can go through all the objects - Katrine commented on April 7th 20 at 15:48

Find more questions by tags OOPPython