Skip to content

abezruchenkov/python-ultimate-guide

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 

Repository files navigation

python-ultimate-guide

Python language concepts guide

Multitasking

pre-emtive multitasking (threading) - os decides to switch tasks external to Python
cooperative multitasking (asyncio) - tasks decide when to give up control
multiprocessing - different processes

multithreading: ThreadPoolExecutor, executor.map, thread_local = threading.local(), race conditions, with threading.Lock() RLock(), Event, Semaphore, Timer, Barrier thread.start() thread.join()

async io: event loop (uvloop), Asyncio.run(), Async with, async for

Memory and GC

reference cycle, gen gc (generations, threshold), gc module (1gen -> 2gen -> 3gen)

Arenas: 256kb, double linked list of usable_arenas (sorted by number of free pools available, the most data closer). Only arenas can truly free memory
Pools: 4kb, dbl linked list to pools with same size class: usedpools (contain data), freepools, full pools
Blocks: linked list - all the same size within pool: untouched, free, allocated

Classes and OOP

ABC.register() make virtual class (not inherited but becomes issubclass()) doesn’t appear in MRO and can’t call super()
_abc_registry - attr in ABC: weakset containing registered virtual classes of current abstract class
ABC.register() cancels subclasshook

Scopes, functions, variables

Iterator protocol: iter() next() raise StopIteration
Generators also have .send() .throw() .close()
i = (yield num) - example of send
coroutine - generator function into which you can pass data

As of Python 3.6, for the CPython implementation of Python, dictionaries remember the order of items inserted. This is considered an implementation detail in Python 3.6;
As of Python 3.7, this is a guaranteed language feature, not merely an implementation detail.

Other

  1. define class Double(metaclass=abc.ABCMeta):
  2. @Double.register
    class CustomDouble:
  3. issubclass(CustomDouble, Double) == True

New Python Features

friendlier error messages
structural pattern matching
type hint improvements (unioin with | list[float | int], TypeAlias, TypeGuards
zip strict param (matching lists are equal)
new funcs in statistics module
modern SSL

Tools

Pympler is a development tool to measure, monitor and analyze the memory behavior of Python objects in a running Python application.
https://pythonhosted.org/Pympler/
cProfile outputs all function calls with timing
import cProfile
cProfile.run('sum([i * 2 for i in range(10000)])')

Bandit - linter for security holes
https://github.com/PyCQA/bandit

About

Python language concepts guide

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published