From 5acb44c8621bec58f96ae86768dd849c6cee6ab8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luiz=20Ot=C3=A1vio?= Date: Tue, 15 Nov 2022 07:22:25 -0300 Subject: [PATCH] method vs @classmethod vs @staticmethod --- aula130.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 aula130.py diff --git a/aula130.py b/aula130.py new file mode 100644 index 0000000..35866bb --- /dev/null +++ b/aula130.py @@ -0,0 +1,39 @@ +# method vs @classmethod vs @staticmethod +# method - self, método de instância +# @classmethod - cls, método de classe +# @staticmethod - método estático (❌self, ❌cls) +class Connection: + def __init__(self, host='localhost'): + self.host = host + self.user = None + self.password = None + + def set_user(self, user): + self.user = user + + def set_password(self, password): + self.password = password + + @classmethod + def create_with_auth(cls, user, password): + connection = cls() + connection.user = user + connection.password = password + return connection + + @staticmethod + def log(msg): + print('LOG:', msg) + + +def connection_log(msg): + print('LOG:', msg) + + +# c1 = Connection() +c1 = Connection.create_with_auth('luiz', '1234') +# c1.set_user('luiz') +# c1.set_password('123') +print(Connection.log('Essa é a mensagem de log')) +print(c1.user) +print(c1.password)