3
Guys, I will gather here in this message two questions of python OO. Anaconda use (python 3.6.1).
I’m trying to make a little project to study object orientation in python 3.6.1. Initially, the problem is when to pass the parameters to the constructor.
# coding: UTF-8
from automovel import Veiculo
class Veiculo(object):
#como atribuo valores de instância para essas variáveis como no java, por exemplo?
placa=None
cor=None
cidade=None
#construtor
def __init__(self, placa, cidade, cor):
self.placa=placa
self.cor=cor
self.cidade=cidade
# a linha abaixo não dá erro, mas não usa o construtor
carro=Veiculo
# a linha abaixo dá erro e nem usa o construtor
carro2 = Veiculo("JFW3128", "Salvador", "preto")
The mistake:
Description Resource Path Location Type Unresolved import: Vehicle auto.py /automoveis line 7 Pydev Problem
Already without the import line, gives the error:
Description Resource Path Location Type Undefined variable: Vehicle car.py /cars line 13 Pydev Problem
Note, the file name is automovel.py. I use the eclipse IDE with pydev installed.
I know there are as many theories in the OO, as in the link:
I prefer to stick to the concrete part.
On this link, it seems to me that python OO works smoothly (python v2):
Print a list of objects as string in Python
Already the same doubt to follow there was no return of user satisfaction:
The file name is
Veiculo
, what matters the moduleVeiculo
(himself) and defines a classVeiculo
? That’s the mistake. What exactly is thisimport
?– Woss
@Anderson Carlos Woss already tried with the different name for the file, but I didn’t notice much change. The Vehicle import is the class and, consequently, the file. I believe that even so should not give error because python does not take into account the file name. There are framework, for example, that have several classes in the same file.
– André Nascimento
The file name will be the module name, so it’s taken into account, yes. The question is, why are you importing the module into itself, since it itself defines the class? The name conflict is between the module name and the class name, it cannot be the same.
– Woss
@Anderson Carlos Woss renewed the project, reviewed the errors and edited the question.
– André Nascimento
But are you still importing the module itself into itself? That doesn’t make sense. Why the
import automovel
within the archive itselfautomovel
?– Woss
@Anderson Carlos Woss why the class import into the file itself is cited in the question, pointing to the second error shown.
– André Nascimento
you are embodying the same class within itself when you report that car 2 = Vehicle ? maybe the error is there, because you may end up generating an eternal loop, try to call out of that class
– Marcos Brinner