__new__ e __init__ em classes Python
This commit is contained in:
@@ -1,7 +1,3 @@
|
|||||||
from eletronico import Smartphone
|
conta_1 = ((1 + 1) ** (5 + 5))
|
||||||
|
|
||||||
galaxy_s = Smartphone('Galaxy S')
|
print(conta_1)
|
||||||
iphone = Smartphone('iPhone')
|
|
||||||
|
|
||||||
galaxy_s.ligar()
|
|
||||||
iphone.desligar()
|
|
||||||
|
|||||||
23
aula148.py
Normal file
23
aula148.py
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
# __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)
|
||||||
Reference in New Issue
Block a user