-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprivate_variabel.py
More file actions
39 lines (30 loc) · 1.1 KB
/
Copy pathprivate_variabel.py
File metadata and controls
39 lines (30 loc) · 1.1 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
"""
_single_leading_underscore
Aturan ini dipakai untuk mendeklarasikan variabel, fungsi, method, atau kelas private di sebuah module.
single_trailingunderscore
Aturan ini dipakai bisanya untuk menhindari konflik penamaan dengan keyword Python atau perintah yang lain.
__double_leading_underscore
Double underscore dipakai untuk menghindari konflik nama atribute antar kelas.
__double_leading_and_trailingunderscore_\
Aturan penulis ini dipakai untuk variabel atau method khusus (yang disebut juga "magic methods")
seperti __init__ dan __len__.
"""
class Hero:
# class variabel
jumlah = 0
__private_jumlah = 10
def __init__(self, name, health):
self.name = name
self.health = health
# variable instance private
self.__private = "private"
# variable instance protected
self._protected = "protected"
lina = Hero("Lina", 100)
print(lina.__dict__)
# private variabel di dalam method atau object
print(lina.__private)
# _protected masih sama halnya dengan variabel public
print(lina._protected)
# private variabel di dalam class
print(Hero.__private_jumlah)