Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a list inside a class static for the entire program

Tags:

python

I'm messing around with classes and data flow and I am having difficulties creating a list of classes inside the class (to give control of the list to the class in itself).

class Person:

    listOfPeople = []

    def __init__(self, name, age):
        self.name = name
        self.age = age
        self.listOfPeople = []
        

    def set_age(self, age):
        if age <= 0:
            raise ValueError('The age must be positive')
        self._age = age

    def get_age(self):
        return self._age
    
    def AppendList(self):
        self.listOfPeople.append(self)

    def returnList(self):
        return self.listOfPeople

    age = property(fget=get_age, fset=set_age)



john = Person('John', 18)

barry = Person("Barry", 19)

john.AppendList()

barry.AppendList()


print(Person.listOfPeople)

The output is simply

[]

Let´s use this example. I want the class Person to have a list of people. That list of people has instances of the class it's in. I want the entire program to have access to this class, regardless of having an instance initialised. Is it even possible to do what I want in Python?

My expected output is a list with the 2 instances I added to the list.

like image 579
CitrusBoy Avatar asked Sep 20 '25 16:09

CitrusBoy


2 Answers

Okay so you need to create the list outside the definitions first.

Then you need to change the append function to this instead, self.listOfPeople.append(self) so that the variables are added into the list when you run it your way and remove the self.listOfPeople from the __init__.

Now the listOfPeople you initialized is shared between all instances.

class Person:
    listOfPeople = []

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

    def set_age(self, age):
        if age <= 0:
            raise ValueError('The age must be positive')
        self._age = age

    def get_age(self):
        return self._age
    
    def AppendList(self):
        self.listOfPeople.append(self)

    def returnList(self):
        return self.listOfPeople

    age = property(fget=get_age, fset=set_age)


john = Person('John', 18)

barry = Person("Barry", 19)

john.AppendList()

barry.AppendList()

print(Person.listOfPeople)

for i in Person.listOfPeople:
    print (i.name, i.age)

Output for first part is:

[<__main__.Person object at 0x7fcb72395150>, <__main__.Person object at 0x7fcb723954d0>]

Output for the second part is:

John 18
Barry 19
like image 72
anarchy Avatar answered Sep 22 '25 05:09

anarchy


this code automatically adds new instances to Person.listOfPeople

class Person:

listOfPeople = []

def __init__(self, name, age):
    self.name = name
    self.age = age
    Person.listOfPeople.append(self)
    

def set_age(self, age):
    if age <= 0:
        raise ValueError('The age must be positive')
    self._age = age

def get_age(self):
    return self._age

def AppendList(self):
    self.listOfPeople.append(self)

def returnList(self):
    return self.listOfPeople

def __repr__(self):             #I've added __repr__
    return self.name+' '+str(self.age)

age = property(fget=get_age, fset=set_age)



john = Person('John', 18)

barry = Person("Barry", 19)

# john.AppendList()

# barry.AppendList()


print(Person.listOfPeople)

the output: [John 18, Barry 19] is this what you need?

like image 42
Dmitriy Neledva Avatar answered Sep 22 '25 05:09

Dmitriy Neledva