0

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.

bradboy
  • 63
  • 7
  • 1
    What do you mean by "*I have a class named People that contains hundreds of instances*"? Are the instances stored in an array? Or how are they "contained"? Can you post the code that created the instances? – Bergi Jul 29 '23 at 23:55

1 Answers1

1

How do I search though all People

Generally, the way to do this is to design the People class so that it keeps track of all instances of itself.

Something like this:

class People:
    crowd = []

    def __init__(self):
        # add this person to the crowd
        People.crowd.append(self)

Now you can iterate over People.crowd and see every instance of People that currently exists in memory.

John Gordon
  • 29,573
  • 7
  • 33
  • 58
  • perfect! Would you happen to know what doing this is called, or how to find reference to learn more about it? – bradboy Jul 30 '23 at 00:25
  • @bradboy I don't know that it has a specific name. See this similar question (I suppose I should have just linked to that as a duplicate, instead of writing up a new answer. Shame on me.) https://stackoverflow.com/q/328851/494134 – John Gordon Jul 30 '23 at 00:33