Encapsulation
What is Encapsulation?
Basic Example
class User:
def __init__(self, username, password):
self.username = username
self.__password = password # private attribute
def check_password(self, password):
return self.__password == password
user = User("alice", "secret123")
print(user.username) # Output: alice
print(user.check_password("wrong")) # Output: False
# print(user.__password) # Error: attribute is privateReal World Use Cases
Key Points
Last updated