Yes, all created objects only exist in RAM memory, and the same is out of place when the program is closed. If you want to persist data, you need to store it in a persistent storage type, such as hard drive or SSD.
However, these forms of storage do not store objects, but rather bytes, therefore, it is necessary to convert the objects you want to store in bytes, in a way that it is possible to convert them back to the objects at another time of the execution of the program. This process is called serialization.
There are several modules to assist in this storage, from simple file manipulation functions to Orms which are object relational models and can be stored in a database with automatic serialization of objects.
In the case of your class, you have placed the reading of the data from the user, on __init__
- This is not very practical as it means that your class cannot be instantiated without asking the user for data. It would be ideal to move the user reading to a separate function, to make it easier to read the data from somewhere other than input
.
Another detail is that you mentioned getters and setters but in python they are not a good practice as in java. You rarely use them in the form of property
, but only when they are really needed.
I end the answer with a simple example to store your class in an SQL database using the library sqlalchemy
:
from sqlalchemy import create_engine, Column, Unicode, Integer
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
engine = create_engine('sqlite:///escola.db', echo=True)
Base = declarative_base(bind=engine)
Session = sessionmaker(bind=engine)
class Diploma(Base):
__tablename__ = 'diplomas'
id = Column(Integer(), primary_key=True)
nome = Column(Unicode(200))
curso = Column(Unicode(200))
registro = Column(Integer())
sistec = Column(Unicode(200))
livro = Column(Integer())
pagina = Column(Integer())
@classmethod
def ler_do_usuario(cls):
d = cls()
d.nome = input("Aluno: ")
d.curso = input("Curso: ")
d.registro = int(input("Registro: "))
d.sistec = input("SISTEC: ")
d.livro = int(input("Livro: "))
d.pagina = int(input("Página: "))
return d
Base.metadata.create_all()
if __name__ == '__main__':
s = Session()
# cria um diploma a partir do usuario:
d = Diploma.ler_do_usuario()
#armazena o diploma no banco de dados:
s.add(d)
s.commit()
# le o primeiro diploma a partir do banco de dados:
d2 = s.query(Diploma).get(1)
print(d2.nome)
Objects are always created in memory, if you want them to persist you need to save them somewhere as a database or file. Now if the question is whether the object representing each diploma is lost while the program is running, it has to do with your code and whether you keep it in some collection
– Isac