A complete guide to learn, implement and development well python code.
- The Python Data Model is the API that we use to make our own objects play well with the most idiomatic language features. The data model is a description of Python as a framework. It formalizes the interfaces of the building blocks of the language itself.
- The Python Data Model: It describes the API that you can use to make your own objects play well with the most idiomatic language
- The dunder methods are so important and you have not touch them
- Collections module provides specialized container datatypes that offer enhanced functionality or performance for specific cases
- Container are data structures that can hold multiple objects or values
- List
- Dicts
- Sets
- Strings
- Tuples
- Remember this three levels:
- First:
- Iterable
__iter__ - Sized
__len__ - and Container
__contains__(to haveinoperation)
- Iterable
- Second:
- Reversible
- and Collections
- Third:
- Sequence,
- Mapping,
- and Set
- First:
- Sequences
- Container: Different types
- Flat: Same tipe and you can use
arraymodule - Mutable: You can change the content
- Immutable: You can not change the content
collections.abcprovides the abstract base classes to implement functions for python containers- listcomp is better than
mapandfilter - Remember: you can use
genexp - Tuples are no just Immutable Lists
- Tuples as records
- Unpack information and match cases like Elixir are best practices
sliceis useful to slice items- Be careful using
+,+=, and*in Sequences, the behaivor can change. import disand usedis.dis()give you a good way to analyze some operationsimport timeitand usetimeit.repeat()can help you to test code snippets- Remember the key differences between
sortandlist.sort collections.dequeandcollectionsmodule (in general) are a good way to perform operations overcollections
- dict comprehension is key in all aspects
**dict_varto unpack dictsd1 | d2to merge dicts- Also you can use Match operations
- The UML information:
- Collections
- Mapping
- MutableMapping
- Hashable is an object that has hash code which never changes during its lifetime:
__hash____eq__
__missing__is a deep way to works withd.get('any', None)- The basic use case for sets is remove duplicatation
- Sets work as math sets with operation like:
- union |
- intersection &
- difference -
- symmetric difference ^
- In general bytes has less size than Unicode
bytes: This is an immutable sequence of bytesbytearray: This is a mutable sequence of bytes- Basic encoders:
- ascii
- latin1
- cp1252
- cp437
- gb2312
- utf-8
- utf-16le
- The Unicode sandwich:
- bytes -> str
- 100% str
- str -> bytes
- If your classes do not have
__repr__or__eq__method it will not be clearly - If your classes do not have
__eq__you can not compare==objects because the comparaison will be over theid - Also you can use
typing.NamedTupleto inheritance dataclassis not immutable, for that reason you have to usefrozen=True__annotations__is useful when you have the type hints__doc__to work with the docs
- Variables are labels, not boxes.
- Aliasing: two variables bound to the same object.
==comparaissonsisto check the identities- To (shallow) copy list you should use
c = list(a) copy()to shallow anddeepcopy()for deep copy.- The only mode of parameter passing in Python is call by sharing
- Mutable types as parameter defaults: Bad Idea
- Objects are never explicity destroyed; however, when they become unreachable they may be garbage-collected
- Higher-Order Function is a function that takes a function as an argument or returns a function as the result.
- The nine Flavors of Callable Objects:
- User-defined functions
- Built-in functions
- Built-in methods
- Methods
- Classes
- Class instances with a
__call__()method - Generator functions
- Native coroutine functions
- Asyncronous generator functions
- Python is gradual typing in practice
flake8,blue, andblackare good options to enhance your code- duck typing in runtime
- normal typing in compile-time
- The function decorators are executed as soon as the module is imported, but the decorated functions only run when they are explicitly invoked.
- It is so important respect
nonlocalvariables and be carefull when and where the var is assigned - The basic decorators does not support
**kwargs - To solve the not
**keywordsargument we can usefunctools - If you will work using recursivity consider to use
@functools.cache
- Strategy pattern is a good point to start
- Command pattern
- Sequence are iterable by the iter Function and the
__getitem__method - Objects implementing an
__iter__method returning an iterator are iterable - Python obtains iterators from iterables
- Iterable class has
__iter__and Iterator class has__next__and__iter__
- collections: it has some common options to manage built-in containers like list, sets, dicts, and tuples
collections.OrderedDictcollections.ChainMapcollections.Countercollections.UserDict: To create your owndictclasses
- random: some operations to create random options
- typing
- dataclasses
- functools: several higher order functions
- operator: intrinsic operators in Python
- typing