Who can access data in objects
- Many object oriented languages have the concept of access control(abstraction). Apply access control modifiers to members of methods
- private: only methods inside object can access them
- protected: only methods inside current class & sub-classes will be able to access
- public: any one who creates object can access
- Python doesn’t do this. In python everything is public.
- Python gives you some conventions which help in making python programmers not use members directly.
- By convention, if we prefix a method or internal attribute (member) with underscore, Python programmer will interpret this as internal variable.
- Whenever you create a class, to mark any thing as internal use underscore as mentioned in the below example
class Credentials: """ Credentials class to hold username & password """ def __init__(self, username='admin', password='admin@123'): """ initilzation of credentials :param username: username :param password: password """ self._username = username self._password = password def username(self): """ Username access property :return: username """ return self._username def password(self): """ Password access property :return: password """ return self._password
- If you want to strongly suggest outside objects don’t access method or property or attribute use double underscore. This will perform name mangling
Third Party Libraries
- Python has very decent Standard Library with lots of packages & modules.
- You can create your own packages or modules or you can probably find the library which you want from Python Package Index (PyPI) Refer Here
- To install these libraries we use a tool called as pip
- Installing pip
python -m ensurepip
- pip is a tool for managing python packages. pip is python’s package manager
- Installing, upgrading & deleting python packages can be done by pip tool
- Lets start with an example package installation called as requests
- now Lets install requests
pip install requests
- Since the request package is downloaded i can use the packages & modules from requests in my python class/script. Lets look at developer interface of requests Refer Here `
- If we start using pip for different projects on the same machine which uses different versions of third party libraries it becomes very much difficult. Python recommends using virtual environment
- Basic purpose of virtual environment is to create an isolate environment for python projects. This means that each project can have its own dependencies
- Creating virtual environments is very easy using virtualenv or pyenv command line tools
- Lets create one virtual environment
mkdir virtual-env cd virtual-env python -m venv env
- Now we can activate this virtual environment using activate scripts