853 lines
37 KiB
Plaintext
853 lines
37 KiB
Plaintext
{
|
||
"metadata": {
|
||
"kernelspec": {
|
||
"name": "python",
|
||
"display_name": "Python (Pyodide)",
|
||
"language": "python"
|
||
},
|
||
"language_info": {
|
||
"codemirror_mode": {
|
||
"name": "python",
|
||
"version": 3
|
||
},
|
||
"file_extension": ".py",
|
||
"mimetype": "text/x-python",
|
||
"name": "python",
|
||
"nbconvert_exporter": "python",
|
||
"pygments_lexer": "ipython3",
|
||
"version": "3.8"
|
||
}
|
||
},
|
||
"nbformat_minor": 5,
|
||
"nbformat": 4,
|
||
"cells": [
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "# Всё в Python является объектом",
|
||
"metadata": {
|
||
"pycharm": {
|
||
"name": "#%% md\n"
|
||
}
|
||
},
|
||
"id": "917bfefc-582c-41b4-b3b2-df29da3c7200"
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "print(isinstance(\"add\", object))\nprint(isinstance(1_000, object))\nprint(isinstance(3.14, object))",
|
||
"metadata": {
|
||
"pycharm": {
|
||
"name": "#%%\n"
|
||
}
|
||
},
|
||
"execution_count": 1,
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": "True\n\nTrue\n\nTrue\n"
|
||
}
|
||
],
|
||
"id": "9aa513ac"
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "(3.14).as_integer_ratio()",
|
||
"metadata": {},
|
||
"execution_count": 3,
|
||
"outputs": [
|
||
{
|
||
"execution_count": 3,
|
||
"output_type": "execute_result",
|
||
"data": {
|
||
"text/plain": [
|
||
"(7070651414971679, 2251799813685248)"
|
||
]
|
||
},
|
||
"metadata": {}
|
||
}
|
||
],
|
||
"id": "3ecb4677"
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "class Vector2D:\n x = 0\n y = 0\n \n def norm(self):\n return (self.x**2 + self.y**2)**0.5\n\nvec = Vector2D()",
|
||
"metadata": {
|
||
"pycharm": {
|
||
"name": "#%%\n"
|
||
}
|
||
},
|
||
"execution_count": 13,
|
||
"outputs": [],
|
||
"id": "b7025d1e-79cf-4ba7-a49d-7c1bf2fe063e"
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "isinstance(vec, object)",
|
||
"metadata": {
|
||
"pycharm": {
|
||
"name": "#%%\n"
|
||
}
|
||
},
|
||
"execution_count": 4,
|
||
"outputs": [
|
||
{
|
||
"execution_count": 4,
|
||
"output_type": "execute_result",
|
||
"data": {
|
||
"text/plain": [
|
||
"True"
|
||
]
|
||
},
|
||
"metadata": {}
|
||
}
|
||
],
|
||
"id": "3371dfc5-234d-481c-8404-f1fd0fd68bb3"
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "isinstance(Vector2D, object)",
|
||
"metadata": {
|
||
"pycharm": {
|
||
"name": "#%%\n"
|
||
}
|
||
},
|
||
"execution_count": 5,
|
||
"outputs": [
|
||
{
|
||
"execution_count": 5,
|
||
"output_type": "execute_result",
|
||
"data": {
|
||
"text/plain": [
|
||
"True"
|
||
]
|
||
},
|
||
"metadata": {}
|
||
}
|
||
],
|
||
"id": "88ccb376-5fbb-47ed-84b5-4773f26d3490"
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "isinstance(object, object)",
|
||
"metadata": {
|
||
"pycharm": {
|
||
"name": "#%%\n"
|
||
}
|
||
},
|
||
"execution_count": 4,
|
||
"outputs": [
|
||
{
|
||
"execution_count": 4,
|
||
"output_type": "execute_result",
|
||
"data": {
|
||
"text/plain": [
|
||
"True"
|
||
]
|
||
},
|
||
"metadata": {}
|
||
}
|
||
],
|
||
"id": "4d77d081"
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "import math\nisinstance(math, object)",
|
||
"metadata": {
|
||
"pycharm": {
|
||
"name": "#%%\n"
|
||
}
|
||
},
|
||
"execution_count": 5,
|
||
"outputs": [
|
||
{
|
||
"execution_count": 5,
|
||
"output_type": "execute_result",
|
||
"data": {
|
||
"text/plain": [
|
||
"True"
|
||
]
|
||
},
|
||
"metadata": {}
|
||
}
|
||
],
|
||
"id": "d4e0bb29-320c-4206-8fc3-4345eda4f73f"
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "def add(a,b): return a + b\nisinstance(add, object)",
|
||
"metadata": {
|
||
"pycharm": {
|
||
"name": "#%%\n"
|
||
}
|
||
},
|
||
"execution_count": 6,
|
||
"outputs": [
|
||
{
|
||
"execution_count": 6,
|
||
"output_type": "execute_result",
|
||
"data": {
|
||
"text/plain": [
|
||
"True"
|
||
]
|
||
},
|
||
"metadata": {}
|
||
}
|
||
],
|
||
"id": "374f4bc2-7bb6-441d-96aa-392445856565"
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "add.x = 1",
|
||
"metadata": {
|
||
"pycharm": {
|
||
"name": "#%%\n"
|
||
}
|
||
},
|
||
"execution_count": 7,
|
||
"outputs": [],
|
||
"id": "0b0b8d31-917e-4fb0-8d41-3e49cc381494"
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "# Магические методы",
|
||
"metadata": {
|
||
"pycharm": {
|
||
"name": "#%% md\n"
|
||
}
|
||
},
|
||
"id": "cce63a23"
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "dir(vec)",
|
||
"metadata": {
|
||
"pycharm": {
|
||
"name": "#%%\n"
|
||
}
|
||
},
|
||
"execution_count": 11,
|
||
"outputs": [
|
||
{
|
||
"execution_count": 11,
|
||
"output_type": "execute_result",
|
||
"data": {
|
||
"text/plain": [
|
||
"['__class__',\n",
|
||
" '__delattr__',\n",
|
||
" '__dict__',\n",
|
||
" '__dir__',\n",
|
||
" '__doc__',\n",
|
||
" '__eq__',\n",
|
||
" '__format__',\n",
|
||
" '__ge__',\n",
|
||
" '__getattribute__',\n",
|
||
" '__gt__',\n",
|
||
" '__hash__',\n",
|
||
" '__init__',\n",
|
||
" '__init_subclass__',\n",
|
||
" '__le__',\n",
|
||
" '__lt__',\n",
|
||
" '__module__',\n",
|
||
" '__ne__',\n",
|
||
" '__new__',\n",
|
||
" '__reduce__',\n",
|
||
" '__reduce_ex__',\n",
|
||
" '__repr__',\n",
|
||
" '__setattr__',\n",
|
||
" '__sizeof__',\n",
|
||
" '__str__',\n",
|
||
" '__subclasshook__',\n",
|
||
" '__weakref__',\n",
|
||
" 'norm',\n",
|
||
" 'x',\n",
|
||
" 'y']"
|
||
]
|
||
},
|
||
"metadata": {}
|
||
}
|
||
],
|
||
"id": "f0cac210-b14c-4940-811f-2f7b982014f5"
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "dir(add)",
|
||
"metadata": {
|
||
"pycharm": {
|
||
"name": "#%%\n"
|
||
}
|
||
},
|
||
"execution_count": 18,
|
||
"outputs": [
|
||
{
|
||
"execution_count": 18,
|
||
"output_type": "execute_result",
|
||
"data": {
|
||
"text/plain": [
|
||
"['__annotations__',\n",
|
||
" '__call__',\n",
|
||
" '__class__',\n",
|
||
" '__closure__',\n",
|
||
" '__code__',\n",
|
||
" '__defaults__',\n",
|
||
" '__delattr__',\n",
|
||
" '__dict__',\n",
|
||
" '__dir__',\n",
|
||
" '__doc__',\n",
|
||
" '__eq__',\n",
|
||
" '__format__',\n",
|
||
" '__ge__',\n",
|
||
" '__get__',\n",
|
||
" '__getattribute__',\n",
|
||
" '__globals__',\n",
|
||
" '__gt__',\n",
|
||
" '__hash__',\n",
|
||
" '__init__',\n",
|
||
" '__init_subclass__',\n",
|
||
" '__kwdefaults__',\n",
|
||
" '__le__',\n",
|
||
" '__lt__',\n",
|
||
" '__module__',\n",
|
||
" '__name__',\n",
|
||
" '__ne__',\n",
|
||
" '__new__',\n",
|
||
" '__qualname__',\n",
|
||
" '__reduce__',\n",
|
||
" '__reduce_ex__',\n",
|
||
" '__repr__',\n",
|
||
" '__setattr__',\n",
|
||
" '__sizeof__',\n",
|
||
" '__str__',\n",
|
||
" '__subclasshook__']"
|
||
]
|
||
},
|
||
"metadata": {}
|
||
}
|
||
],
|
||
"id": "1b5597dc-484a-43d3-b37a-cd30908d495d"
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "* Управляют внутренней работой объектов\n* Хранят различную информацию объектов (которую можно получать в runtime)\n* Вызываются при использовании синтаксических конструкций\n* Вызываются встроенными (builtins) функциями\n* Область применения: перегрузка операторов, рефлексия и метапрограммирование",
|
||
"metadata": {
|
||
"pycharm": {
|
||
"name": "#%% md\n"
|
||
}
|
||
},
|
||
"id": "bc3cdb2f"
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "class TenItemList:\n\n def __len__(self):\n return 10\n\n\nten_item_list = TenItemList()\nlen(ten_item_list)",
|
||
"metadata": {
|
||
"pycharm": {
|
||
"name": "#%%\n"
|
||
}
|
||
},
|
||
"execution_count": 8,
|
||
"outputs": [
|
||
{
|
||
"execution_count": 8,
|
||
"output_type": "execute_result",
|
||
"data": {
|
||
"text/plain": [
|
||
"10"
|
||
]
|
||
},
|
||
"metadata": {}
|
||
}
|
||
],
|
||
"id": "dabd01d8"
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "# Всё в Python является объектом, а все синтаксические конструкции сводятся к вызовам магических методов",
|
||
"metadata": {
|
||
"pycharm": {
|
||
"name": "#%% md\n"
|
||
}
|
||
},
|
||
"id": "0effc8ba"
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "# Пример сложение\n",
|
||
"metadata": {
|
||
"pycharm": {
|
||
"name": "#%% md\n"
|
||
}
|
||
},
|
||
"id": "3841bda9"
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "class Vector2D:\n\n def __init__(self, x, y):\n self.x = x\n self.y = y\n\n def __add__(self, other):\n return Vector2D(self.x + other.y, self.x + other.y)\n\n def norm(self):\n return (self.x**2 + self.y**2)**0.5\n\nvec1 = Vector2D(1,2)\nvec2 = Vector2D(3,4)\nvec3 = vec1 + vec2\nvec3.x, vec3.y",
|
||
"metadata": {
|
||
"pycharm": {
|
||
"name": "#%%\n"
|
||
}
|
||
},
|
||
"execution_count": 10,
|
||
"outputs": [
|
||
{
|
||
"execution_count": 10,
|
||
"output_type": "execute_result",
|
||
"data": {
|
||
"text/plain": [
|
||
"(5, 5)"
|
||
]
|
||
},
|
||
"metadata": {}
|
||
}
|
||
],
|
||
"id": "e3460fe5"
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "## Пример присваивание",
|
||
"metadata": {
|
||
"pycharm": {
|
||
"name": "#%% md\n"
|
||
}
|
||
},
|
||
"id": "538a0794"
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "class Vector2D:\n x = 0\n y = 0\n \n def norm(self):\n return (self.x**2 + self.y**2)**0.5\n\nvec = Vector2D()",
|
||
"metadata": {},
|
||
"execution_count": 14,
|
||
"outputs": [],
|
||
"id": "1acd0880"
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "vec = Vector2D()\nvec.__getattribute__(\"x\")",
|
||
"metadata": {
|
||
"pycharm": {
|
||
"name": "#%%\n"
|
||
}
|
||
},
|
||
"execution_count": 15,
|
||
"outputs": [
|
||
{
|
||
"execution_count": 15,
|
||
"output_type": "execute_result",
|
||
"data": {
|
||
"text/plain": [
|
||
"0"
|
||
]
|
||
},
|
||
"metadata": {}
|
||
}
|
||
],
|
||
"id": "1f2bfb38"
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "vec.__getattribute__(\"norm\")()",
|
||
"metadata": {},
|
||
"execution_count": 17,
|
||
"outputs": [
|
||
{
|
||
"execution_count": 17,
|
||
"output_type": "execute_result",
|
||
"data": {
|
||
"text/plain": [
|
||
"0.0"
|
||
]
|
||
},
|
||
"metadata": {}
|
||
}
|
||
],
|
||
"id": "640d5fad"
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "vec.x = 5\nvec.__getattribute__(\"x\")",
|
||
"metadata": {
|
||
"pycharm": {
|
||
"name": "#%%\n"
|
||
}
|
||
},
|
||
"execution_count": 18,
|
||
"outputs": [
|
||
{
|
||
"execution_count": 18,
|
||
"output_type": "execute_result",
|
||
"data": {
|
||
"text/plain": [
|
||
"5"
|
||
]
|
||
},
|
||
"metadata": {}
|
||
}
|
||
],
|
||
"id": "21efb174"
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "vec.__setattr__(\"x\", 10)\ngetattr(vec, \"x\")",
|
||
"metadata": {
|
||
"pycharm": {
|
||
"name": "#%%\n"
|
||
}
|
||
},
|
||
"execution_count": 19,
|
||
"outputs": [
|
||
{
|
||
"execution_count": 19,
|
||
"output_type": "execute_result",
|
||
"data": {
|
||
"text/plain": [
|
||
"10"
|
||
]
|
||
},
|
||
"metadata": {}
|
||
}
|
||
],
|
||
"id": "dfbce5a2"
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "setattr(vec, \"x\", 20)\nvec.x",
|
||
"metadata": {
|
||
"pycharm": {
|
||
"name": "#%%\n"
|
||
}
|
||
},
|
||
"execution_count": 20,
|
||
"outputs": [
|
||
{
|
||
"execution_count": 20,
|
||
"output_type": "execute_result",
|
||
"data": {
|
||
"text/plain": [
|
||
"20"
|
||
]
|
||
},
|
||
"metadata": {}
|
||
}
|
||
],
|
||
"id": "1d6de7d3"
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "class Foo:\n def __setattr__(self, key, value):\n print(key, value)\n\nfoo = Foo()\nfoo.a = \"A\"",
|
||
"metadata": {
|
||
"pycharm": {
|
||
"name": "#%%\n"
|
||
}
|
||
},
|
||
"execution_count": 21,
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": "a A\n"
|
||
}
|
||
],
|
||
"id": "40df4888"
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "foo.a",
|
||
"metadata": {
|
||
"pycharm": {
|
||
"name": "#%%\n"
|
||
}
|
||
},
|
||
"execution_count": 22,
|
||
"outputs": [
|
||
{
|
||
"ename": "AttributeError",
|
||
"evalue": "'Foo' object has no attribute 'a'",
|
||
"output_type": "error",
|
||
"traceback": [
|
||
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
||
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
|
||
"\u001b[0;32m/tmp/ipykernel_35789/2615815247.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mfoo\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
|
||
"\u001b[0;31mAttributeError\u001b[0m: 'Foo' object has no attribute 'a'"
|
||
]
|
||
}
|
||
],
|
||
"id": "871b0b35"
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "",
|
||
"metadata": {
|
||
"pycharm": {
|
||
"name": "#%% md\n"
|
||
}
|
||
},
|
||
"id": "13c80f91"
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "# На самом деле все объекты реализованы как словари хранящие атрибуты объекта (однако есть возможности для оптимизаций)",
|
||
"metadata": {
|
||
"pycharm": {
|
||
"name": "#%% md\n"
|
||
}
|
||
},
|
||
"id": "266ca832"
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "class Vector2D:\n x = 0\n y = 0\n\n def norm(self):\n return (self.x**2 + self.y**2)**0.5\n\nvec = Vector2D()",
|
||
"metadata": {
|
||
"pycharm": {
|
||
"name": "#%%\n"
|
||
}
|
||
},
|
||
"execution_count": 23,
|
||
"outputs": [],
|
||
"id": "af1b83e9"
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "vec.__dict__",
|
||
"metadata": {
|
||
"pycharm": {
|
||
"name": "#%%\n"
|
||
}
|
||
},
|
||
"execution_count": 24,
|
||
"outputs": [
|
||
{
|
||
"execution_count": 24,
|
||
"output_type": "execute_result",
|
||
"data": {
|
||
"text/plain": [
|
||
"{}"
|
||
]
|
||
},
|
||
"metadata": {}
|
||
}
|
||
],
|
||
"id": "acc6da9e"
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "Vector2D.__dict__",
|
||
"metadata": {
|
||
"pycharm": {
|
||
"name": "#%%\n"
|
||
}
|
||
},
|
||
"execution_count": 27,
|
||
"outputs": [
|
||
{
|
||
"execution_count": 27,
|
||
"output_type": "execute_result",
|
||
"data": {
|
||
"text/plain": [
|
||
"mappingproxy({'__module__': '__main__',\n",
|
||
" 'x': 0,\n",
|
||
" 'y': 0,\n",
|
||
" 'norm': <function __main__.Vector2D.norm(self)>,\n",
|
||
" '__dict__': <attribute '__dict__' of 'Vector2D' objects>,\n",
|
||
" '__weakref__': <attribute '__weakref__' of 'Vector2D' objects>,\n",
|
||
" '__doc__': None})"
|
||
]
|
||
},
|
||
"metadata": {}
|
||
}
|
||
],
|
||
"id": "4cfbfaec"
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "vec.x = 5\nvec.__dict__",
|
||
"metadata": {
|
||
"pycharm": {
|
||
"name": "#%%\n"
|
||
}
|
||
},
|
||
"execution_count": 26,
|
||
"outputs": [
|
||
{
|
||
"execution_count": 26,
|
||
"output_type": "execute_result",
|
||
"data": {
|
||
"text/plain": [
|
||
"{'x': 5}"
|
||
]
|
||
},
|
||
"metadata": {}
|
||
}
|
||
],
|
||
"id": "b5eb0e56"
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "# Модуль inspect --- информация об объектах в runtime\n\n* Не вся информация может быть доступна через магические методы\n* Недоступную информацию можно получить через модуль inspect",
|
||
"metadata": {
|
||
"pycharm": {
|
||
"name": "#%% md\n"
|
||
}
|
||
},
|
||
"id": "f4af619e"
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "import inspect\n\ndef add(a,b): return a + b\ninspect.isfunction(add)",
|
||
"metadata": {
|
||
"pycharm": {
|
||
"name": "#%%\n"
|
||
}
|
||
},
|
||
"execution_count": 28,
|
||
"outputs": [
|
||
{
|
||
"execution_count": 28,
|
||
"output_type": "execute_result",
|
||
"data": {
|
||
"text/plain": [
|
||
"True"
|
||
]
|
||
},
|
||
"metadata": {}
|
||
}
|
||
],
|
||
"id": "cb6a08ad"
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "inspect.getsource(add)",
|
||
"metadata": {
|
||
"pycharm": {
|
||
"name": "#%%\n"
|
||
}
|
||
},
|
||
"execution_count": 29,
|
||
"outputs": [
|
||
{
|
||
"execution_count": 29,
|
||
"output_type": "execute_result",
|
||
"data": {
|
||
"text/plain": [
|
||
"'def add(a,b): return a + b\\n'"
|
||
]
|
||
},
|
||
"metadata": {}
|
||
}
|
||
],
|
||
"id": "3bd00ea2"
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "from numpy import random\ninspect.getsource(random)",
|
||
"metadata": {
|
||
"pycharm": {
|
||
"name": "#%%\n"
|
||
}
|
||
},
|
||
"execution_count": 30,
|
||
"outputs": [
|
||
{
|
||
"execution_count": 30,
|
||
"output_type": "execute_result",
|
||
"data": {
|
||
"text/plain": [
|
||
"'\"\"\"\\n========================\\nRandom Number Generation\\n========================\\n\\nUse ``default_rng()`` to create a `Generator` and call its methods.\\n\\n=============== =========================================================\\nGenerator\\n--------------- ---------------------------------------------------------\\nGenerator Class implementing all of the random number distributions\\ndefault_rng Default constructor for ``Generator``\\n=============== =========================================================\\n\\n============================================= ===\\nBitGenerator Streams that work with Generator\\n--------------------------------------------- ---\\nMT19937\\nPCG64\\nPCG64DXSM\\nPhilox\\nSFC64\\n============================================= ===\\n\\n============================================= ===\\nGetting entropy to initialize a BitGenerator\\n--------------------------------------------- ---\\nSeedSequence\\n============================================= ===\\n\\n\\nLegacy\\n------\\n\\nFor backwards compatibility with previous versions of numpy before 1.17, the\\nvarious aliases to the global `RandomState` methods are left alone and do not\\nuse the new `Generator` API.\\n\\n==================== =========================================================\\nUtility functions\\n-------------------- ---------------------------------------------------------\\nrandom Uniformly distributed floats over ``[0, 1)``\\nbytes Uniformly distributed random bytes.\\npermutation Randomly permute a sequence / generate a random sequence.\\nshuffle Randomly permute a sequence in place.\\nchoice Random sample from 1-D array.\\n==================== =========================================================\\n\\n==================== =========================================================\\nCompatibility\\nfunctions - removed\\nin the new API\\n-------------------- ---------------------------------------------------------\\nrand Uniformly distributed values.\\nrandn Normally distributed values.\\nranf Uniformly distributed floating point numbers.\\nrandom_integers Uniformly distributed integers in a given range.\\n (deprecated, use ``integers(..., closed=True)`` instead)\\nrandom_sample Alias for `random_sample`\\nrandint Uniformly distributed integers in a given range\\nseed Seed the legacy random number generator.\\n==================== =========================================================\\n\\n==================== =========================================================\\nUnivariate\\ndistributions\\n-------------------- ---------------------------------------------------------\\nbeta Beta distribution over ``[0, 1]``.\\nbinomial Binomial distribution.\\nchisquare :math:`\\\\\\\\chi^2` distribution.\\nexponential Exponential distribution.\\nf F (Fisher-Snedecor) distribution.\\ngamma Gamma distribution.\\ngeometric Geometric distribution.\\ngumbel Gumbel distribution.\\nhypergeometric Hypergeometric distribution.\\nlaplace Laplace distribution.\\nlogistic Logistic distribution.\\nlognormal Log-normal distribution.\\nlogseries Logarithmic series distribution.\\nnegative_binomial Negative binomial distribution.\\nnoncentral_chisquare Non-central chi-square distribution.\\nnoncentral_f Non-central F distribution.\\nnormal Normal / Gaussian distribution.\\npareto Pareto distribution.\\npoisson Poisson distribution.\\npower Power distribution.\\nrayleigh Rayleigh distribution.\\ntriangular Triangular distribution.\\nuniform Uniform distribution.\\nvonmises Von Mises circular distribution.\\nwald Wald (inverse Gaussian) distribution.\\nweibull Weibull distribution.\\nzipf Zipf\\'s distribution over ranked data.\\n==================== =========================================================\\n\\n==================== ==========================================================\\nMultivariate\\ndistributions\\n-------------------- ----------------------------------------------------------\\ndirichlet Multivariate generalization of Beta distribution.\\nmultinomial Multivariate generalization of the binomial distribution.\\nmultivariate_normal Multivariate generalization of the normal distribution.\\n==================== ==========================================================\\n\\n==================== =========================================================\\nStandard\\ndistributions\\n-------------------- ---------------------------------------------------------\\nstandard_cauchy Standard Cauchy-Lorentz distribution.\\nstandard_exponential Standard exponential distribution.\\nstandard_gamma Standard Gamma distribution.\\nstandard_normal Standard normal distribution.\\nstandard_t Standard Student\\'s t-distribution.\\n==================== =========================================================\\n\\n==================== =========================================================\\nInternal functions\\n-------------------- ---------------------------------------------------------\\nget_state Get tuple representing internal state of generator.\\nset_state Set state of generator.\\n==================== =========================================================\\n\\n\\n\"\"\"\\n__all__ = [\\n \\'beta\\',\\n \\'binomial\\',\\n \\'bytes\\',\\n \\'chisquare\\',\\n \\'choice\\',\\n \\'dirichlet\\',\\n \\'exponential\\',\\n \\'f\\',\\n \\'gamma\\',\\n \\'geometric\\',\\n \\'get_state\\',\\n \\'gumbel\\',\\n \\'hypergeometric\\',\\n \\'laplace\\',\\n \\'logistic\\',\\n \\'lognormal\\',\\n \\'logseries\\',\\n \\'multinomial\\',\\n \\'multivariate_normal\\',\\n \\'negative_binomial\\',\\n \\'noncentral_chisquare\\',\\n \\'noncentral_f\\',\\n \\'normal\\',\\n \\'pareto\\',\\n \\'permutation\\',\\n \\'poisson\\',\\n \\'power\\',\\n \\'rand\\',\\n \\'randint\\',\\n \\'randn\\',\\n \\'random\\',\\n \\'random_integers\\',\\n \\'random_sample\\',\\n \\'ranf\\',\\n \\'rayleigh\\',\\n \\'sample\\',\\n \\'seed\\',\\n \\'set_state\\',\\n \\'shuffle\\',\\n \\'standard_cauchy\\',\\n \\'standard_exponential\\',\\n \\'standard_gamma\\',\\n \\'standard_normal\\',\\n \\'standard_t\\',\\n \\'triangular\\',\\n \\'uniform\\',\\n \\'vonmises\\',\\n \\'wald\\',\\n \\'weibull\\',\\n \\'zipf\\',\\n]\\n\\n# add these for module-freeze analysis (like PyInstaller)\\nfrom . import _pickle\\nfrom . import _common\\nfrom . import _bounded_integers\\n\\nfrom ._generator import Generator, default_rng\\nfrom .bit_generator import SeedSequence, BitGenerator\\nfrom ._mt19937 import MT19937\\nfrom ._pcg64 import PCG64, PCG64DXSM\\nfrom ._philox import Philox\\nfrom ._sfc64 import SFC64\\nfrom .mtrand import *\\n\\n__all__ += [\\'Generator\\', \\'RandomState\\', \\'SeedSequence\\', \\'MT19937\\',\\n \\'Philox\\', \\'PCG64\\', \\'PCG64DXSM\\', \\'SFC64\\', \\'default_rng\\',\\n \\'BitGenerator\\']\\n\\n\\ndef __RandomState_ctor():\\n \"\"\"Return a RandomState instance.\\n\\n This function exists solely to assist (un)pickling.\\n\\n Note that the state of the RandomState returned here is irrelevant, as this\\n function\\'s entire purpose is to return a newly allocated RandomState whose\\n state pickle can set. Consequently the RandomState returned by this function\\n is a freshly allocated copy with a seed=0.\\n\\n See https://github.com/numpy/numpy/issues/4763 for a detailed discussion\\n\\n \"\"\"\\n return RandomState(seed=0)\\n\\n\\nfrom numpy._pytesttester import PytestTester\\ntest = PytestTester(__name__)\\ndel PytestTester\\n'"
|
||
]
|
||
},
|
||
"metadata": {}
|
||
}
|
||
],
|
||
"id": "e2e6f3f7"
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"source": "# Модуль inspect --- информация об объектах в runtime\n\n* Не вся информация может быть доступна через магические методы\n* Недоступную информацию можно получить через модуль inspect",
|
||
"metadata": {
|
||
"pycharm": {
|
||
"name": "#%% md\n"
|
||
}
|
||
},
|
||
"id": "8c0cfc80"
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "import inspect\n\ndef add(a,b): return a + b\ninspect.isfunction(add)",
|
||
"metadata": {
|
||
"pycharm": {
|
||
"name": "#%%\n"
|
||
}
|
||
},
|
||
"execution_count": 2,
|
||
"outputs": [
|
||
{
|
||
"execution_count": 2,
|
||
"output_type": "execute_result",
|
||
"data": {
|
||
"text/plain": [
|
||
"True"
|
||
]
|
||
},
|
||
"metadata": {}
|
||
}
|
||
],
|
||
"id": "f49084f4"
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "inspect.getsource(add)",
|
||
"metadata": {
|
||
"pycharm": {
|
||
"name": "#%%\n"
|
||
}
|
||
},
|
||
"execution_count": 12,
|
||
"outputs": [
|
||
{
|
||
"execution_count": 12,
|
||
"output_type": "execute_result",
|
||
"data": {
|
||
"text/plain": [
|
||
"'def add(a,b): return a + b\\n'"
|
||
]
|
||
},
|
||
"metadata": {}
|
||
}
|
||
],
|
||
"id": "7de58194"
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "from numpy import random\ninspect.getsource(random)",
|
||
"metadata": {
|
||
"pycharm": {
|
||
"name": "#%%\n"
|
||
}
|
||
},
|
||
"execution_count": 3,
|
||
"outputs": [
|
||
{
|
||
"execution_count": 3,
|
||
"output_type": "execute_result",
|
||
"data": {
|
||
"text/plain": [
|
||
"'\"\"\"\\n========================\\nRandom Number Generation\\n========================\\n\\nUse ``default_rng()`` to create a `Generator` and call its methods.\\n\\n=============== =========================================================\\nGenerator\\n--------------- ---------------------------------------------------------\\nGenerator Class implementing all of the random number distributions\\ndefault_rng Default constructor for ``Generator``\\n=============== =========================================================\\n\\n============================================= ===\\nBitGenerator Streams that work with Generator\\n--------------------------------------------- ---\\nMT19937\\nPCG64\\nPCG64DXSM\\nPhilox\\nSFC64\\n============================================= ===\\n\\n============================================= ===\\nGetting entropy to initialize a BitGenerator\\n--------------------------------------------- ---\\nSeedSequence\\n============================================= ===\\n\\n\\nLegacy\\n------\\n\\nFor backwards compatibility with previous versions of numpy before 1.17, the\\nvarious aliases to the global `RandomState` methods are left alone and do not\\nuse the new `Generator` API.\\n\\n==================== =========================================================\\nUtility functions\\n-------------------- ---------------------------------------------------------\\nrandom Uniformly distributed floats over ``[0, 1)``\\nbytes Uniformly distributed random bytes.\\npermutation Randomly permute a sequence / generate a random sequence.\\nshuffle Randomly permute a sequence in place.\\nchoice Random sample from 1-D array.\\n==================== =========================================================\\n\\n==================== =========================================================\\nCompatibility\\nfunctions - removed\\nin the new API\\n-------------------- ---------------------------------------------------------\\nrand Uniformly distributed values.\\nrandn Normally distributed values.\\nranf Uniformly distributed floating point numbers.\\nrandom_integers Uniformly distributed integers in a given range.\\n (deprecated, use ``integers(..., closed=True)`` instead)\\nrandom_sample Alias for `random_sample`\\nrandint Uniformly distributed integers in a given range\\nseed Seed the legacy random number generator.\\n==================== =========================================================\\n\\n==================== =========================================================\\nUnivariate\\ndistributions\\n-------------------- ---------------------------------------------------------\\nbeta Beta distribution over ``[0, 1]``.\\nbinomial Binomial distribution.\\nchisquare :math:`\\\\\\\\chi^2` distribution.\\nexponential Exponential distribution.\\nf F (Fisher-Snedecor) distribution.\\ngamma Gamma distribution.\\ngeometric Geometric distribution.\\ngumbel Gumbel distribution.\\nhypergeometric Hypergeometric distribution.\\nlaplace Laplace distribution.\\nlogistic Logistic distribution.\\nlognormal Log-normal distribution.\\nlogseries Logarithmic series distribution.\\nnegative_binomial Negative binomial distribution.\\nnoncentral_chisquare Non-central chi-square distribution.\\nnoncentral_f Non-central F distribution.\\nnormal Normal / Gaussian distribution.\\npareto Pareto distribution.\\npoisson Poisson distribution.\\npower Power distribution.\\nrayleigh Rayleigh distribution.\\ntriangular Triangular distribution.\\nuniform Uniform distribution.\\nvonmises Von Mises circular distribution.\\nwald Wald (inverse Gaussian) distribution.\\nweibull Weibull distribution.\\nzipf Zipf\\'s distribution over ranked data.\\n==================== =========================================================\\n\\n==================== ==========================================================\\nMultivariate\\ndistributions\\n-------------------- ----------------------------------------------------------\\ndirichlet Multivariate generalization of Beta distribution.\\nmultinomial Multivariate generalization of the binomial distribution.\\nmultivariate_normal Multivariate generalization of the normal distribution.\\n==================== ==========================================================\\n\\n==================== =========================================================\\nStandard\\ndistributions\\n-------------------- ---------------------------------------------------------\\nstandard_cauchy Standard Cauchy-Lorentz distribution.\\nstandard_exponential Standard exponential distribution.\\nstandard_gamma Standard Gamma distribution.\\nstandard_normal Standard normal distribution.\\nstandard_t Standard Student\\'s t-distribution.\\n==================== =========================================================\\n\\n==================== =========================================================\\nInternal functions\\n-------------------- ---------------------------------------------------------\\nget_state Get tuple representing internal state of generator.\\nset_state Set state of generator.\\n==================== =========================================================\\n\\n\\n\"\"\"\\n__all__ = [\\n \\'beta\\',\\n \\'binomial\\',\\n \\'bytes\\',\\n \\'chisquare\\',\\n \\'choice\\',\\n \\'dirichlet\\',\\n \\'exponential\\',\\n \\'f\\',\\n \\'gamma\\',\\n \\'geometric\\',\\n \\'get_state\\',\\n \\'gumbel\\',\\n \\'hypergeometric\\',\\n \\'laplace\\',\\n \\'logistic\\',\\n \\'lognormal\\',\\n \\'logseries\\',\\n \\'multinomial\\',\\n \\'multivariate_normal\\',\\n \\'negative_binomial\\',\\n \\'noncentral_chisquare\\',\\n \\'noncentral_f\\',\\n \\'normal\\',\\n \\'pareto\\',\\n \\'permutation\\',\\n \\'poisson\\',\\n \\'power\\',\\n \\'rand\\',\\n \\'randint\\',\\n \\'randn\\',\\n \\'random\\',\\n \\'random_integers\\',\\n \\'random_sample\\',\\n \\'ranf\\',\\n \\'rayleigh\\',\\n \\'sample\\',\\n \\'seed\\',\\n \\'set_state\\',\\n \\'shuffle\\',\\n \\'standard_cauchy\\',\\n \\'standard_exponential\\',\\n \\'standard_gamma\\',\\n \\'standard_normal\\',\\n \\'standard_t\\',\\n \\'triangular\\',\\n \\'uniform\\',\\n \\'vonmises\\',\\n \\'wald\\',\\n \\'weibull\\',\\n \\'zipf\\',\\n]\\n\\n# add these for module-freeze analysis (like PyInstaller)\\nfrom . import _pickle\\nfrom . import _common\\nfrom . import _bounded_integers\\n\\nfrom ._generator import Generator, default_rng\\nfrom .bit_generator import SeedSequence, BitGenerator\\nfrom ._mt19937 import MT19937\\nfrom ._pcg64 import PCG64, PCG64DXSM\\nfrom ._philox import Philox\\nfrom ._sfc64 import SFC64\\nfrom .mtrand import *\\n\\n__all__ += [\\'Generator\\', \\'RandomState\\', \\'SeedSequence\\', \\'MT19937\\',\\n \\'Philox\\', \\'PCG64\\', \\'PCG64DXSM\\', \\'SFC64\\', \\'default_rng\\',\\n \\'BitGenerator\\']\\n\\n\\ndef __RandomState_ctor():\\n \"\"\"Return a RandomState instance.\\n\\n This function exists solely to assist (un)pickling.\\n\\n Note that the state of the RandomState returned here is irrelevant, as this\\n function\\'s entire purpose is to return a newly allocated RandomState whose\\n state pickle can set. Consequently the RandomState returned by this function\\n is a freshly allocated copy with a seed=0.\\n\\n See https://github.com/numpy/numpy/issues/4763 for a detailed discussion\\n\\n \"\"\"\\n return RandomState(seed=0)\\n\\n\\nfrom numpy._pytesttester import PytestTester\\ntest = PytestTester(__name__)\\ndel PytestTester\\n'"
|
||
]
|
||
},
|
||
"metadata": {}
|
||
}
|
||
],
|
||
"id": "8fd68968"
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"source": "",
|
||
"metadata": {
|
||
"pycharm": {
|
||
"name": "#%%\n"
|
||
}
|
||
},
|
||
"execution_count": null,
|
||
"outputs": [],
|
||
"id": "3d84fffb"
|
||
}
|
||
]
|
||
} |