Python Notes (1)

In order for the light to shine so brightly, the darkness must be present.

Francis Bacon

Create virtual environment (Windows)

1
2
3
4
5
6
7
8
# Create the virtual env
python -m venv DIR\NAME_OF_PROJECT
# Create the virtual env with basic python packages
python -m venv DIR\NAME_OF_PROJECT --system-site-packages
# Active the virtual env
DIR\NAME_OF_PROJECT\Scripts\activate.bat
# Deactivate
deactivate

Python Immutable

From the Python Glossary :

An object is hashable if it has a hash value which never changes during its lifetime.

# Notes

# the object need __hash__() method
# the object can be compared to other onjects (__eq__() or __cmp__() method)

All of Python immutable built-in objects are hashable (such as string, tuple), while no mutable containers (such as lists or dictionaries) are not hashable. Objects which are instances of user-defined classes are hashable by default; they all compare unequal, and their hash value is their id().

Hashability makes an object usable as a dictionary key and a set member, because these data structures use the hash value internally. Therefore, in dictionary, the key can be tuple or string but not list.