Import in Python

Asked

Viewed 3,354 times

0

How do I import in Python being this class created by me?

Example: in java I create a connection class and I can import it in the stock class to be able to persist in the bank, but I don’t know how to do this in Python and I see examples on the internet but modules, I haven’t seen classes yet.

1 answer

3

If the file containing the class is in the same directory as the file that will import just:

from nome_do_arquivo import Nome_da_classe_nesse_arquivo

Example we have 2 files in the same place called myClose.py and the other main py.

In myClose.py we would have the following code:

class ClasseQueNaoFazNada():
    def nada():
        print("Nada")

And in the archive main py. let’s call the created class and run its method using this code:

from minhaClasse import ClasseQueNaoFazNada

ClasseQueNaoFazNada.nada()

So we can import and use a class created by ourselves.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.