Skip to content

Instantly share code, notes, and snippets.

@artsiomkaltovich
Last active November 22, 2022 21:39
Show Gist options
  • Save artsiomkaltovich/8cd9dd2ec2756ccd123b5873b031d56c to your computer and use it in GitHub Desktop.
Save artsiomkaltovich/8cd9dd2ec2756ccd123b5873b031d56c to your computer and use it in GitHub Desktop.
интересности питона
https://stackoverflow.com/questions/33945261/how-to-specify-multiple-return-types-using-type-hints
https://youtu.be/SJ8z-TF07s4?t=17m55s - Удаление(очищение) __dict__
https://youtu.be/SJ8z-TF07s4?t=1h21m22s - можно задавать свои строки формата
# упрощение кода
https://speakerdeck.com/pyconslides/transforming-code-into-beautiful-idiomatic-python-by-raymond-hettinger-1
https://gist.github.com/0x4D31/f0b633548d8e0cfb66ee3bea6a0deff9
yeild и yeild from как операторы, хз зачем :)
https://youtu.be/SuRVWRRjb_U?t=14m22s - import from * испортирует не все ф-ции, а описанные в __all__
https://youtu.be/SuRVWRRjb_U?t=1h7m26s - как разрешить кольцевые ссылки.
https://youtu.be/y78VXTI8PRQ?t=5m28s - doctest
https://www.python.org/dev/peps/pep-0465/
https://www.python.org/dev/peps/pep-0211/
https://youtu.be/mTp2pLEoDaI?t=1h14m38s - от стандартных коллекций лучше не наследоваться, для этого есть collections.abc
https://youtu.be/mTp2pLEoDaI?t=1h17m35s - "уплощение" коллекции
https://docs.python.org/3/library/math.html#math.fsum - точная вещ. сумма
https://youtu.be/anrOzOapJ2E?t=30m30s - параметры с приоритетами, но без копирования.
https://youtu.be/anrOzOapJ2E?t=37m18s - вычислять лучше в строку, а не в столбик)
https://www.python.org/dev/peps/pep-0570/ - только позиционные операторы
https://www.python.org/dev/peps/pep-0412/
https://www.python.org/dev/peps/pep-0428/ - работа с путями
https://www.python.org/dev/peps/pep-0519/
3.8
а в питоне есть объект Ellipsis, в коде обозначается ...
Можно сделать так
from typing import runtime_checkable, Protocol
@runtime_checkable
class Callable(Protocol):
def __call__(self): ...
assert isinstance(print, Callable)
интерфейсы добрались до python, постепенно всё становится джавой)
Кроме самой джавы
А ещё ускорили копирование файлов)
The speedup for copying a 512 MiB file within the same partition is about +26% on Linux, +50% on macOS and +40% on Windows.
The list constructor does not overallocate the internal item buffer if the input iterable has a known length (the input implements __len__). This makes the created list 12% smaller on average. (Contributed by Raymond Hettinger and Pablo Galindo in bpo-33234.)
Меняем батарейки на ходу
>>> from statistics import mean
>>> mean(data=[10, 20, 90])
40
>>> mean.__code__ = mean.__code__.replace(co_posonlyargcount=1)
>>> mean(data=[10, 20, 90])
Traceback (most recent call last):
...
TypeError: mean() got some positional-only arguments passed as keyword arguments: 'data'
https://twitter.com/raymondh/status/1217161890553425920
вопрос на завалить кандидата)
https://www.python.org/dev/peps/pep-0420/ - namespace пакеты, могут собираться из физически разных папок, если в них не объявлены __init__.py, полезно при написании библиотек, поддерживающих плагины
pep-8 говорит, что стандартная библиотека не должна использовать локальные импорты, asincio на это плевать.
>>> a = ([],)
>>> a[0] += [1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> a[0]
[1] # исключение бросается, но данные уже изменены
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment