Encapsulation describes the idea of wrapping data and the methods that work on data within one unit. This puts restrictions on accessing variables and methods directly and can prevent the accidental modification of data
- The goal of information hiding is to ensure that an object’s state is always valid by controlling access to attributes that are hidden from the outside world.
Access modifiers: Python doesn't have any mechanism that effectively restricts access to any instance variable or method. Python prescribes a convention of prefixing the name of the variable/method with a single (_) or double underscore (__) to emulate the behavior of protected and private access specifiers.
- Public
- Protected: to add a prefix
_(single underscore) to it- Protected members of a class are accessible from within the class and are also available to its sub-classes. No other environment is permitted access to it.
- Although the protected variable can be accessed out of the class as well as in the derived class (modified too in derived class), it is customary(convention not a rule) to not access the protected out the class body.
- Private:
- The double underscore
__prefixed to a variable makes it private. It gives a strong suggestion not to touch it from outside the class. Any attempt to do so will result in anAttributeError - Name mangling: Every member with a double underscore will be changed to
_object._<class_name>__<private_attribute>. So, it can still be accessed from outside the class, but the practice should be refrained.
- The double underscore
- Common Package: Attrs, Pydantic, or Python Data Classes
Pydanticis a Python library for data modeling/parsing that has efficient error handling and a custom validation mechanism.
Dataclasses, as the name clearly suggests, are classes that are meant to hold data. The motivation behind this module is that we sometimes define classes that only act as data containers and when we do that, we spend a consequent amount of time writing boilerplate code with tons of arguments, an ugly __init__ method and many overridden functions.
- Type annotation for each attribute. Although this doesn’t enforce type validation, it helps your text editor provide better linting
- dataclass decorator is actually a code generator that automatically adds other methods under the hood:
__init__,__eq__and__repr__methods: these methods are responsible for setting the attribute values, testing for equality and representing objects in a nice string format.
class Person():
def __init__(self, first_name, last_name, age, job):
self.first_name = first_name
self.last_name = last_name
self.age = age
self.job = job
# dataclass help to remove ugly __init__ method
from dataclasses import dataclass
@dataclass
class Person:
first_name: str
last_name: str
age: int
job: str- The basic idea is that we write tests even before we write code. The tests define what it means to say that our program works “correctly.”
- Test templates:
- The first test in every exercise checks whether the expected program exists.
- The second test checks that the program will print a help message if we ask for help.
- After that, your program will be run with various inputs and options.
- Step 1:
python3 -m venv venvfor initialising the virtual environment - Step 2: Activating the virtual environment
- Linux or MacOS
source venv/bin/activate - Window
venv/venv\Scripts\activate.bat
- Linux or MacOS
The following commands shall be ran after activating the virtual environment.
pip install --upgrade pipfor upgrading the pippip install -r requirements.txtfor the functional dependenciespip install -r requirements-dev.txtfor the development dependencies. (should includepre-commitmodule)