-2
I created a simple class, I can create an object, but I’m developing a CRUD that needs to create the object from a function. Look at my example:
class Funcionario:
def __init__(self, nome, cargo, salario):
self.nome = nome
self.cargo = cargo
self.salario = salario
def registrar():
func = Funcionario(
nome = input('Nome: '),
cargo = input('Cargo: '),
salario=(input('Salário: '))
)
def modificar():
func ?
The problem is that the creation of the object stays within the function, that is, it is not global.
If I try to create a method change in the class and I will run from a function edit the position, for example, how do I instantiate the object, being that it is in the other function? And if I leave out, he keeps calling all the time the creation of the object, which I don’t want.
Complete code:
class Funcionario:
def __init__(self, nome, cargo, salario):
self.nome = nome
self.cargo = cargo
self.salario = salario
def altera_cargo(self):
novo_cargo = input('Novo cargo: ')
self.cargo = novo_cargo
print('Cargo atualizado para {}'.format(self.cargo))
def altera_salario(self):
novo_salario = float(input('Novo salário: '))
self.salario = novo_salario
print('Salário atualizado para R$ {}'.format(self.salario))
def registra():
return Funcionario(
nome = input('Nome: '),
cargo = input('Cargo: '),
salario = float(input('Salário: '))
)
def menu():
opcao = int(input('1. Novo\n2. Editar cargo\n3. Editar salário\n--> '))
if opcao == 1:
registra()
print('\n')
menu()
if opcao == 2:
altera_cargo() # como acessar o objeto ?
print('\n')
menu()
if opcao == 3:
altera_salario() # como acessar o objeto ?
print('\n')
menu()
else:
exit()
if __name__ == "__main__":
menu()
I answered your comment with a question, sorry. Can you take a look at the question panel?
– Tguitarful
@Tguitarful In fact you used the answer field, which is intended only for solutions. The right thing is you edit the question and put the code there (I already did, but next time, edit the question itself) - I also updated my answer
– hkotsubo
Oops, the way you showed it worked really, thank you! Sorry to be insistent, is that the code is getting more and more complex. The main idea is to separate it. So for example, as the function was tied to the menu function, it was inaccessible outside. The intention now is to create a function for each task, create, modify, delete and query. The object that is created needs to be accessible to all these functions, since the menu will now be in a separate file and will call these functions when necessary. As well as write to the database as well.
– Tguitarful
@Tguitarful Please understand that the format of [pt.so] is for questions/answers. You ask a specific question and people answer only that, nothing else. It’s not like in a forum, where the discussion can lengthen and the problem can change. So if you have another question (even if it is related to this one), the best is ask another question. But in general, it would be enough to do the job
menu
return the employee. There who call themenu
takes the turn (func1 = menu()
, afterwardfunc2 = menu()
, etc.)...– hkotsubo
...and you use these employees any way you want. Of course, there are many ways to do it, so maybe another question is better, because then the answers can be more focused. But anyway, if the answer solved your problem (which is in the question, not the other one you just mentioned), you can accept it, see here how and why to do it. It is not mandatory, but it is a good practice of the site, to indicate to future visitors that it solved the problem. As for the other problem, I suggest you ask another question...
– hkotsubo
...which is better, not only to keep the site more organized - a question by specific problem - but also because new questions become visible on the main page, then everyone can see and you have more chances to have an answer. Putting in the comments, less people will see.
– hkotsubo
Yes, I’m still getting used to the format of the site. Anyway vlw by the help. In the original post, your answer solved the problem! Thank you for the force!
– Tguitarful