Python: How to correctly import a method that depends on another?

2

I'm restarting a project I did in Java, trying to learn about Python in POO, but I'm stuck on something I have not found any solution to:

I have 2 files ("working.py" and ".py files").

The 2nd is responsible for any services related to file (writing, testing, reading ...), all cute inside a class named Files.

But when I try to import somehow it works I get an error from some function that has not been defined. The overwhelming majority of functions within .py files depends on a name function .

working.py:

"""==========
  trabalhando.py
=========="""
from arquivos import Arquivos

a = Arquivos()
print(a.le("banco.txt")) #Uma lista do tamanho da qtd de linhas

files.py:

"""==========
  arquivos.py
=========="""
class Arquivos:
    #testes
    def existe (nome): 
        try: 
            with open(nome, "r") as arquivo:
                pass 
        except IOError: 
            return (nome, "w")

        arquivo.close
        return (nome, "a")

    #leitura de arquivo
    def le (arquivo):
        objeto = existe(arquivo)  ##<= linha problema
        if (objeto == (arquivo, "w")):
            return []

        file = open(objeto[0], "r")
        texto = []

        for i in file:
            if (i == "\n"):
                continue
            texto.insert(len(texto), i[:i.index("\n")])
            if (len(texto)%136 == 0):
                print(aguarde(len(texto)))

        file.close
        return texto

So, how do I call the .py file (.py file) function without it pointing me to "exists not defined" problem?

    
asked by anonymous 25.09.2016 / 21:46

1 answer

3

Functions inside classes are called methods, and they must have as the first parameter the value self and after it the parameter that will be passed to the method.

All your methods within the class add self as the first parameter.

After this in the line that gives error is because as you are inside the same class the way to call the method that is in the same place (class) is like this:

self.metodo_in_the same_class (parameters)

So in your case it would be:

#leitura de arquivo
def le(self,arquivo):
    objeto = self.existe(arquivo)

Remembering that if the existe method throws an exception in the event of an error, the le method must use a try to catch the generated error.

    
25.09.2016 / 23:48