forked from mouredev/roadmap-retos-programacion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mouredev.py
68 lines (48 loc) · 1.25 KB
/
mouredev.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""
Ejercicio
"""
class Singleton:
_instance = None
def __new__(cls):
if not cls._instance:
cls._instance = super(Singleton, cls).__new__(cls)
return cls._instance
singleton1 = Singleton()
print(singleton1)
singleton2 = Singleton()
print(singleton2)
print(singleton1 is singleton2)
"""
Extra
"""
class UserSession:
_instance = None
id: int = None
username: str = None
name: str = None
email: str = None
def __new__(cls):
if not cls._instance:
cls._instance = super(UserSession, cls).__new__(cls)
return cls._instance
def set_user(self, id, username, name, email):
self.id = id
self.username = username
self.name = name
self.email = email
def get_user(self):
return f"{self.id}, {self.username}, {self.name}, {self.email}"
def clear_user(self):
self.id = None
self.username = None
self.name = None
self.email = None
session1 = UserSession()
print(session1.get_user())
session1.set_user(1, "mouredev", "Brais Moure", "[email protected]")
print(session1.get_user())
session2 = UserSession()
print(session2.get_user())
session3 = UserSession()
session3.clear_user()
print(session3.get_user())