Files
cursopython2023/aula148.py
2022-11-19 19:09:44 -03:00

24 lines
640 B
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# __new__ e __init__ em classes Python
# __new__ é o método responsável por criar e
# retornar o novo objeto. Por isso, new recebe cls.
# __new__ ❗DEVE retornar o novo objeto❗
# __init__ é o método responsável por inicializar
# a instância. Por isso, init recebe self.
# __init__ ❗NÃO DEVE retornar nada (None)❗️
# object é a super classe de uma classe
class A:
def __new__(cls, *args, **kwargs):
instancia = super().__new__(cls)
return instancia
def __init__(self, x):
self.x = x
print('Sou o init')
def __repr__(self):
return 'A()'
a = A(123)
print(a.x)