2
I have a Shop class that one of her attributes is a list (self.products = [ ]).
class Loja(Empresa):
def __init__(self, nome, cnpj, razao_social):
super().__init__(nome, cnpj)
self._razao_social = razao_social
self._funcionarios = []
self._clientes = []
self.produtos = []
Outside of this class I have a method def report that receives as parameter a type of product (electronic, food etc) and goes through this list of products and prints the names of the products.
def relatorio(tipo_produto):
for i in Loja.produtos:
if isinstance(tipo_produto, Produto):
print(i)
The problem is that this way I did not work, the pycharm returns an error saying that "the Store object type was not assigned to Products". If you can help, I really appreciate it.
self.produtos
is not an attribute of its class, it is an attribute of instances that you create from the class. You cannot accessLoja.produto
because this value does not exist at the class level, only of the instances created from it. The correct one would be to initialize an instance ofLoja
with something likeminha_loja = Loja(nome='Supermercado', ...)
and then access the attributeminha_loja.produtos
.– jfaccioni
If one of the answers below solved your problem and there was no doubt left, choose the one you liked the most and mark it as correct/accepted by clicking on the " " that is next to it, which also marks your question as solved. If you still have any questions or would like further clarification, feel free to comment.
– Lucas