0

I have a class Person with the attributes name and level

class Person:

    def __init__(self, name, level):
            
        self.name = name
        self.level = level

let's say i have a bunch of objects from that class with different attributes

p1 = Person("person1", "5")
p2 = Person("person2", "10")
p3 = Person("person2", "15")

And a list containing the name of all those objects

people_list = ["p1","p2","p3"]

i want to make a function on the Person class, that finds and prints all the levels This is what i made so far.

def lvlFinder(self, people_list):
        
        for x, item in enumerate(people_list):
            y = getattr(p1, "level")
            print(y)

Is there a way so instead of p1 in getattr(p1,"level") i could have a variable that changes to p2 and so on as it loops. I tried many different ways, but honestly i don't even know if anything is right. Is there a better way of doing this?

v1sor1
  • 3
  • 1
  • 3
    "And a list containing the name of all those objects" Your list *does not contain those objects*. It contains *strings*. Just put those objects in the list and use the objects in a loop. – juanpa.arrivillaga Nov 07 '22 at 19:21
  • Does `people_list` contain the _variable names_, or the `name` attribute of the `Person` objects? If it is the former, _why_?! What is the point in having the list contain _strings_ that tell you the name of the _variable_ that your object is assigned to? Why not simply have a list containing the `Person` objects in the first place? If it is the latter, you're going to have to figure out a way to look at all `Person` objects that have been created so far to find which one(s) have that name. Better to just make the list a list of `Person` objects – Pranav Hosangadi Nov 07 '22 at 19:23
  • It was what juanpa.arrivillaga said, Thank you. – v1sor1 Nov 07 '22 at 19:31
  • Note, `getattr` never made any sense here. `getattr(p1, "level")` should just be `p1.level` – juanpa.arrivillaga Nov 07 '22 at 19:36

2 Answers2

1

You have a few faults, each of which is obscuring other parts of the program.

Start by making a list of the people:

people_list = [p1, p2, p3]

Then you can iterate directly over the list:

def lvlFinder(people_list):
        for item in people_list:
            y = item.level
            print(y)

Finally you just have to call the function:

lvlFinder(people_list)
quamrana
  • 37,849
  • 12
  • 53
  • 71
  • Thank you i knew there was a lot of extra stuff that i didn't need, the reason i was struggling with this i think it was because the original code was so much bigger, and in pure python i could make it work but then when i tried to translate to renpy multi class system i was using, it was giving me errors that i didn't know if it was renpy or python really. THX – v1sor1 Nov 07 '22 at 19:43
0

Your people_list variable should contain the Person objects instead of strings. Then you can loop over the list and get the levels like this:

people_list = [p1, p2, p3]

for person in people_list:
    print(person.level)

getattr() really is not needed or useful in this case.

user99999
  • 310
  • 1
  • 13