From 28cc7f2ea4aa2b2efc6dfedf42f47b288c2715b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luiz=20Ot=C3=A1vio?= Date: Sat, 19 Nov 2022 19:09:44 -0300 Subject: [PATCH] __new__ e __init__ em classes Python --- aula141/main.py | 8 ++------ aula148.py | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 aula148.py diff --git a/aula141/main.py b/aula141/main.py index 35e993a..6c4d372 100644 --- a/aula141/main.py +++ b/aula141/main.py @@ -1,7 +1,3 @@ -from eletronico import Smartphone +conta_1 = ((1 + 1) ** (5 + 5)) -galaxy_s = Smartphone('Galaxy S') -iphone = Smartphone('iPhone') - -galaxy_s.ligar() -iphone.desligar() +print(conta_1) diff --git a/aula148.py b/aula148.py new file mode 100644 index 0000000..7249fea --- /dev/null +++ b/aula148.py @@ -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)