Skip to content

Instantly share code, notes, and snippets.

@p2004a
Last active August 29, 2015 14:17
Show Gist options
  • Save p2004a/7efe2fea338704f13ed9 to your computer and use it in GitHub Desktop.
Save p2004a/7efe2fea338704f13ed9 to your computer and use it in GitHub Desktop.
Python tutorial for programmers
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "",
"signature": "sha256:ee3afb478aefea0f63fc531afa4ca37187feb0e1321fb0d1ead85c19689104a3"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Python cz\u0119\u015b\u0107 4"
]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Dekoratory"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"W internecie jest a\u017c za du\u017co miejsc gdzie mo\u017cna przeczyta\u0107 jak to dzia\u0142a."
]
},
{
"cell_type": "heading",
"level": 4,
"metadata": {},
"source": [
"Proste dekoratory"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def wykonajdwarazy(func):\n",
" def wrapper(*args, **kwargs):\n",
" func(*args, **kwargs)\n",
" func(*args, **kwargs)\n",
" \n",
" return wrapper\n",
"\n",
"@wykonajdwarazy\n",
"def f(n):\n",
" print(n)\n",
"\n",
"f(10)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"10\n",
"10\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def g(n):\n",
" print(\"g\", n)\n",
"\n",
"g = wykonajdwarazy(g)\n",
"\n",
"g(1)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"g 1\n",
"g 1\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"@wykonajdwarazy\n",
"@wykonajdwarazy\n",
"def f(n):\n",
" print(n)\n",
" \n",
"f(10)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"10\n",
"10\n",
"10\n",
"10\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "heading",
"level": 4,
"metadata": {},
"source": [
"Dekoratory przyjmuj\u0105ce argumenty"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def my_filter(*args):\n",
" def decorator(func):\n",
" def wrapper(arg):\n",
" if arg in args:\n",
" return func(arg)\n",
" return None\n",
" return wrapper\n",
" return decorator\n",
"\n",
"@my_filter(1,2,3)\n",
"def f(n):\n",
" print(n)\n",
"\n",
"f(10)\n",
"f(1)\n",
"f(5)\n",
"f(2)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1\n",
"2\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "heading",
"level": 4,
"metadata": {},
"source": [
"Dekorowanie klas"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def c(cls):\n",
" class klass(cls):\n",
" def __init__(self, *args, **kwargs):\n",
" super().__init__(*args, **kwargs)\n",
" print(\"init\")\n",
" \n",
" return klass\n",
"\n",
"@c\n",
"class B:\n",
" pass\n",
"\n",
"a = B()\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"init\n"
]
}
],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"class D(B):\n",
" pass\n",
"\n",
"d = D()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"init\n"
]
}
],
"prompt_number": 6
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"S\u0142\u00f3wka kluczowe `global` i `nonlocal`"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"a = 5\n",
"\n",
"def f():\n",
" global a\n",
" a += 1\n",
" \n",
"f()\n",
"f()\n",
"f()\n",
"print(a)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"8\n"
]
}
],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def f():\n",
" counter = 0\n",
" \n",
" def n():\n",
" nonlocal counter\n",
" counter += 1\n",
" return counter\n",
" \n",
" return n\n",
"\n",
"c = f()\n",
"\n",
"print(c())\n",
"print(c())\n",
"print(f())\n",
"\n",
"d = f()\n",
"print(d())\n",
"print(d())\n",
"\n",
"print(f()())"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1\n",
"2\n",
"<function f.<locals>.n at 0x2b0a4f971f28>\n",
"1\n",
"2\n",
"1\n"
]
}
],
"prompt_number": 8
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Adnotacje"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"https://www.python.org/dev/peps/pep-3107/"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def f(a : 3, b : \"asdasd\", d : 1 = 10) -> \"asdasd\":\n",
" \"asdasdasdasd\"\n",
" pass\n",
"\n",
"print(f.__annotations__)\n",
"print(f.__doc__)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"{'b': 'asdasd', 'return': 'asdasd', 'd': 1, 'a': 3}\n",
"asdasdasdasd\n"
]
}
],
"prompt_number": 9
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Deskryptory"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Polecam artyku\u0142: http://nbviewer.ipython.org/gist/ChrisBeaumont/5758381/descriptor_writeup.ipynb"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"class A:\n",
" def __init__(self):\n",
" self.a = 0\n",
" \n",
" @property\n",
" def a_plus_one(self):\n",
" return self.a + 1\n",
" \n",
" @a_plus_one.setter\n",
" def a_plus_one(self, value):\n",
" self.a = value - 1\n",
" \n",
"a = A()\n",
"a.a = 10\n",
"print(a.a_plus_one)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"11\n"
]
}
],
"prompt_number": 10
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"class D:\n",
" def __init__(self, name):\n",
" self.name = name\n",
" \n",
" def __get__(self, obj, cls):\n",
" return self.name\n",
" \n",
" def __set__(self, obj, value):\n",
" self.name = value\n",
"\n",
"\n",
"class A:\n",
" d = D(\"marek\")\n",
"\n",
"a = A()\n",
"print(a.d)\n",
"a.d = 10\n",
"print(a.d)\n",
"\n",
"b = A()\n",
"print(a.d)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"marek\n",
"10\n",
"10\n"
]
}
],
"prompt_number": 11
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 11
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment