-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_private.py
More file actions
54 lines (40 loc) · 1.53 KB
/
class_private.py
File metadata and controls
54 lines (40 loc) · 1.53 KB
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
'''
Атрибути, що вважаються приватними позначаються двома підкресленнями __ і не можуть бути доступні безпосередньо ззовні класу.
'''
# class Person:
# def __init__(self, name: str, age: int, is_active: bool, is_admin: bool):
# self.name = name
# self.age = age
# self._is_active = is_active
# self.__is_admin = is_admin
# def greeting(self):
# return f"Hi {self.name}"
# def is_active(self):
# return self._is_active
# def set_active(self, active: bool):
# self._is_active = active
# p = Person("Boris", 34, True, False)
# print(p.__is_admin)
'''
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[6], line 13
9 return f"Hi {self.name}"
12 p = Person("Boris", 34, True, False)
---> 13 print(p.__is_admin)
AttributeError: 'Person' object has no attribute '__is_admin'
'''
class Person:
def __init__(self, name: str, age: int, is_active: bool, is_admin: bool):
self.name = name
self.age = age
self._is_active = is_active
self.__is_admin = is_admin
def greeting(self):
return f"Hi {self.name}"
def is_active(self):
return self._is_active
def set_active(self, active: bool):
self._is_active = active
p = Person("Boris", 34, True, False)
print(p._Person__is_admin) # False