Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit efe413d

Browse files
committed
update modules and mangling name in class
1 parent 9cc7fce commit efe413d

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

README.md

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1488,6 +1488,14 @@ Simply create a file with a `.py` extension.
14881488
```python
14891489
# transaction.py
14901490

1491+
# variable in modules
1492+
mock = {
1493+
"tranId": "random_id",
1494+
"amount": 0.0,
1495+
"tax": 0.0,
1496+
"address": "123 Sub St",
1497+
}
1498+
14911499
def add(amount: int, tax: float, address: str) -> str:
14921500
return f'tranId: random_id, {amount =}, {tax =}, {address =}'
14931501

@@ -1502,6 +1510,7 @@ To use a module, import it into your script using the `import` statement
15021510
```python
15031511
import transaction
15041512

1513+
print(transaction.mock) # {'tranId': 'random_id', 'amount': 0.0, 'tax': 0.0, 'address': '123 Sub St'}
15051514
print(transaction.add(10, 20.0, "nowhere"))
15061515
print(transaction.update("011010", 10, 20.0, "nowhere"))
15071516
```
@@ -1581,6 +1590,20 @@ import math # this is math.py because it have higher priority
15811590
print(math.add(1, 2))
15821591
```
15831592

1593+
#### Built-in modules
1594+
1595+
There are several built-in modules in Python, which you can import whenever you like, you can find them in here: https://docs.python.org/3/py-modindex.html
1596+
1597+
```python
1598+
import platform
1599+
import math
1600+
import io
1601+
1602+
print(platform.system())
1603+
print(math.sqrt(12))
1604+
print(io.open("contacts.txt").readline())
1605+
```
1606+
15841607
### Classes
15851608

15861609
Python is an Object Oriented Programming language.
@@ -1760,7 +1783,7 @@ student_tuan.name = "Kim Phuong"
17601783
student_tuan.welcome() # Welcome Kim Phuong to the class A3
17611784
```
17621785

1763-
#### Class access control
1786+
#### Access Modifiers
17641787

17651788
Python supports a form of access control for class properties and methods, although it does so in a more informal way compared to some other languages like Java or C++.
17661789

@@ -1811,9 +1834,10 @@ Python use naming conventions to indicate the intended level of access:
18111834
obj.access_protected()
18121835

18131836
# example 02
1814-
# we still able to access private attributes and methods but will breaks the encapsulation principle
1815-
# by using mangled name
1816-
# Not recommended to use this way.
1837+
# Python doesn't block access to private data, it just leaves for the wisdom of the programmer,
1838+
# not to write any code that access it from outside the class.
1839+
# You can still access the private attributes/methods by Python's name mangling technique
1840+
# but will breaks the encapsulation principle so not recommend to do this in real-life
18171841
class SubClass(MyClass):
18181842
def access_protected(self):
18191843
print(self._MyClass__private_var) # I am protected

0 commit comments

Comments
 (0)