OOP02_Encapsulation_Private_attributes.py
python_old/OOP02_Encapsulation_Private_attributes.py
## In python class, how many double underscore methods are available? What their purpose on each? - Homework #1
class BankAccount: ## Object class
def __init__(self, owner, balance):
self.owner = owner
self.__balance = balance # private attribute
def deposit(self, amount):
self.__balance += amount
def withdraw(self, amount):
if amount <= self.__balance:
self.__balance -= amount
else:
print("Insufficient funds.")
def get_balance(self):
return self.__balance
if __name__ == "__main__":
account = BankAccount("John Doe", 1000)
# account.__balance = 2000 # Attempt to modify private attribute directly
# print(account.__balance) # This will not print the actual balance
account.deposit(500)
account.withdraw(200)
print(account.get_balance())
OOP01_Class_Definition_and_Object_Instantiation.py
Next →OOP03_Class_Instance_Variables_Methods.py
Related articles
01_classical_caesar_cipher.py
01_classical_caesar_cipher.py — python source code from the python old learning materials (python_old/Cryptography/01_classical_caesar_cipher.py).
Read article →02_classical_monoalphabetic.py
02_classical_monoalphabetic.py — python source code from the python old learning materials (python_old/Cryptography/02_classical_monoalphabetic.py).
Read article →03_classical_rail_fence.py
03_classical_rail_fence.py — python source code from the python old learning materials (python_old/Cryptography/03_classical_rail_fence.py).
Read article →04_frequency_analysis.py
04_frequency_analysis.py — python source code from the python old learning materials (python_old/Cryptography/04_frequency_analysis.py).
Read article →05_cryptographic_hashing.py
05_cryptographic_hashing.py — python source code from the python old learning materials (python_old/Cryptography/05_cryptographic_hashing.py).
Read article →06_password_hashing.py
06_password_hashing.py — python source code from the python old learning materials (python_old/Cryptography/06_password_hashing.py).
Read article →