I don't know exactly how to articulate what I'm trying to accomplish.
Let's assume I have a class named People that contains hundreds of instances. One attribute of People is email_address. Another attribute is name.
How do I search though all People to find the attribute 'email_address' that has a value of 'spam_me@email.com' and have the People.name of that instance returned so I can do whatever with that specific instance?
In my specific use case, the class is actually for similar industrial machines, and one machine out of many is the 'lead' and the others are the 'slaves'. I won't always know which is the lead, because they all have the ability to be lead. Which machine is lead rotates so that all machines have similar wear and hours on them. The slaves only turn on to do their thing when lead is not able to keep up with demand.
I like SQL, so this specific question, in SQL, would look something like so:
SELECT name FROM People WHERE email_address='spam_me@email.com';
Along the same train of thought, I don't necessarily always need the Person.name returned to me. At times, it would be just as well to replace spam_me@email.com with no@domain.tld
Again, in SQL, it would look something like so:
UPDATE People SET email_address=REPLACE(email_address,'spam_me@email.com','no@domain.tld');
Sorry about the SQL references, I'm just trying to illustrate what I'm hoping to achieve!
( BTW... if you're experimenting with SQL, that 2nd example code could be roughly translated into a text editors' search & replace all matches. )
In response to John Gordon:
3 class Unit:
4 units = []
5 def __init__(self, name, relay_term, ol_term):
6 self.name = name
7 self.ol_term = ol_term
8 self.relay_term = relay_term
9 self.role = 'unknown'
~~~
18 Unit.units.append(self)
~~~
36
37 @classmethod
38 def find(cls, role):
39 for unit in Unit.units:
40 if unit.role == role:
41 return unit.name
42
43 left = Unit('left',15,12)
44 middle = Unit('middle',25,22)
45 right = Unit('right',35,32)
46
47 middle.role = 'lead'
48
49 for unit in Unit.units:
50 if unit.role == 'lead':
51 print(unit.name)
52
53 print(Unit.find('lead'))
Beautiful! Thanks, John!
For the people that are trying to solve a similar issue, lines 37 through 41 are creating a function inside of a class (known as a "method". Line 37 specifically is telling python the next function belongs to the class itself, not an instance of the class. This is known as a "class method". Line 53 calls that class method.