Error: Exception has occurred: Typeerror (Python)

Asked

Viewed 70 times

1

I have the following error when compiling:

Exception has occurred: TypeError
__init__() missing 2 required positional arguments: 'usuario' and 'senha'

What I’m doing wrong?

class Login:

    usuario = str(input("Digite seu usuário: "))
    senha = input("Digite sua senha: ")

    def __init__(self, usuario, senha):
        self.usuario = usuario
        self.senha = senha

    def retorno_usuario(self):
        return self.usuario

    def retorno_senha(self):
        return self.senha


log = Login()

print("O usuário e: {}").format(log.usuario)

print("O usuário e: {}").format(log.senha)

1 answer

2


First you are not compiling, because python is an interpreted language, you are running a script. Second, you are initiating the object Login no arguments however specified in the function __init__ that would start with user and password. To correct your error do log = Login('novo-usuario', 'nova-senha')

The python constructor method is called __init__ and is always called when we try to create an object from a class, example:

class Foo:
    def __init__(self, foo)
        print(foo)

bar = Foo('python')
  • Thanks, man!

  • The following is an interesting reading on "interpretation vs compilation": https://stackoverflow.com/a/6889798

  • I agree that the vm python transpila the code for bytecode at runtime however this does not make python a compiled language since it does not generate a binary that after the build process will be executed by the target processor. Python is a interpreted language that transpilada for bytecode and tbm can be compiled in C.

Browser other questions tagged

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