0
I am having problems with the inheritance of attributes between a mother class and a daughter class that are in two different files.
When using them in the same file no error is displayed:
cls_usuario.py file:
class Usuario:
def __init__(self, id_usuario, nome, login, senha, tipo_conta, departamento, cargo, situacao):
self._id_usuario = id_usuario
self._nome_usuario = nome
self._login_usuario = login
self._senha_usuario = senha
self._tipo_conta_usuario = tipo_conta
self._departamento_usuario = departamento
self._cargo_usuario = cargo
self._situacao_usuario = situacao
class Administrador(cls_usuario.Usuario):
def __init__(self, id_administrador):
super().__init__(id_usuario, nome, login, senha, tipo_conta, departamento, cargo, situacao)
self.id_administrador = id_administrador
The problem arises when I try to separate the two classes into different files, because I would like to make my project more organized, besides having to use the mother class in several other classes.
cls_usuario.py file - version 2:
class Usuario:
def __init__(self, id_usuario, nome, login, senha, tipo_conta, departamento, cargo, situacao):
self._id_usuario = id_usuario
self._nome_usuario = nome
self._login_usuario = login
self._senha_usuario = senha
self._tipo_conta_usuario = tipo_conta
self._departamento_usuario = departamento
self._cargo_usuario = cargo
self._situacao_usuario = situacao
cls_admin.py file:
import cls_usuario
class Administrador(cls_usuario.Usuario):
def __init__(self, id_administrador):
#super().__init__(id_usuario, nome, login, senha, tipo_conta, departamento, cargo, situacao)
self.id_administrador = id_administrador
Something important to remember in python is that files do not represent organization. In the vast majority of times separating into files will leave more messy than organized. Always look to split into files when code responsibilities are different, when they do completely different things. This is so true in the language that it is quite common for you to see entire libraries implemented in just one file.
– Woss
On the issue, consider: to create a user I would need to provide name, login, password, etc. To create an administrator I would only need the id. What would be the name of this administrator? You called the method
__init__
passing variables that do not exist in the classAdministrador
.– Woss
The way you posted - apart from the wrong identations, this error will not occur - I suppose in the code that has these errors, your line calling
super
Don’t comment on it, okay? (The serros alias that are not even of the language, are of some tool of code verification - the language would give a Nameerror in the first variable inexistent without indicating the others)– jsbueno