{"id":290257,"date":"2018-10-03T16:00:02","date_gmt":"2018-10-03T12:00:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=290257"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=290257","title":{"rendered":"\u041f\u043e\u0434\u0431\u043e\u0440\u043a\u0430 @pythonetc, \u0441\u0435\u043d\u0442\u044f\u0431\u0440\u044c 2018"},"content":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\">\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/79\/ur\/ug\/79urugmgbgzjhlpozccip51e5_s.png\"><\/p>\n<p>  <\/p>\n<p>\u042d\u0442\u043e \u0447\u0435\u0442\u0432\u0451\u0440\u0442\u0430\u044f \u043f\u043e\u0434\u0431\u043e\u0440\u043a\u0430 \u0441\u043e\u0432\u0435\u0442\u043e\u0432 \u043f\u0440\u043e Python \u0438 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438\u0437 \u043c\u043e\u0435\u0433\u043e \u0430\u0432\u0442\u043e\u0440\u0441\u043a\u043e\u0433\u043e \u043a\u0430\u043d\u0430\u043b\u0430 <a href=\"http:\/\/t.me\/pythonetc\">@pythonetc<\/a>.<\/p>\n<p>  <\/p>\n<p>\u041f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0435 \u043f\u043e\u0434\u0431\u043e\u0440\u043a\u0438:<\/p>\n<p>  <\/p>\n<ul>\n<li><a href=\"https:\/\/habr.com\/company\/mailru\/blog\/422789\/\">\u0410\u0432\u0433\u0443\u0441\u0442 2018<\/a><\/li>\n<li><a href=\"https:\/\/habr.com\/company\/mailru\/blog\/419025\/\">\u0418\u044e\u043b\u044c 2018<\/a><\/li>\n<li><a href=\"https:\/\/habr.com\/company\/mailru\/blog\/416605\/\">\u0418\u044e\u043d\u044c 2018<\/a><\/li>\n<\/ul>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<h2 id=\"pereopredelenie-i-peregruzka\">\u041f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0438 \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043a\u0430<\/h2>\n<p>  <\/p>\n<p>\u0421\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0434\u0432\u0435 \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043b\u0435\u0433\u043a\u043e \u0441\u043f\u0443\u0442\u0430\u0442\u044c: \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 (overriding) \u0438 \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043a\u0430 (overloading).<\/p>\n<p>  <\/p>\n<p>\u041f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0441\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u043a\u043b\u0430\u0441\u0441 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u043c\u0435\u0442\u043e\u0434, \u0443\u0436\u0435 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0439 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u043c\u0438 \u043a\u043b\u0430\u0441\u0441\u0430\u043c\u0438, \u0438 \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u0437\u0430\u043c\u0435\u043d\u044f\u0435\u0442 \u0435\u0433\u043e. \u0412 \u043a\u0430\u043a\u0438\u0445-\u0442\u043e \u044f\u0437\u044b\u043a\u0430\u0445 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u044f\u0432\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043f\u043e\u043c\u0435\u0447\u0430\u0442\u044c \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044e\u0449\u0438\u0439 \u043c\u0435\u0442\u043e\u0434 (\u0432 C# \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 <code>override<\/code>), \u0430 \u0432 \u043a\u0430\u043a\u0438\u0445-\u0442\u043e \u044f\u0437\u044b\u043a\u0430\u0445 \u044d\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u043f\u043e \u0436\u0435\u043b\u0430\u043d\u0438\u044e (\u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044f <code>@Override<\/code> \u0432 Java). Python \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439 \u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u0438 \u043d\u0435 \u043f\u0440\u0435\u0434\u0443\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0435\u0442 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0439 \u043f\u043e\u043c\u0435\u0442\u043a\u0438 \u0442\u0430\u043a\u0438\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 (\u043a\u0442\u043e-\u0442\u043e \u0440\u0430\u0434\u0438 \u0447\u0438\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0439 \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440 <code>@override<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0434\u0435\u043b\u0430\u0435\u0442).<\/p>\n<p>  <\/p>\n<p>\u0421 \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043a\u043e\u0439 \u0434\u0440\u0443\u0433\u0430\u044f \u0438\u0441\u0442\u043e\u0440\u0438\u044f. \u042d\u0442\u0438\u043c \u0442\u0435\u0440\u043c\u0438\u043d\u043e\u043c \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442\u0441\u044f \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044f, \u043a\u043e\u0433\u0434\u0430 \u0435\u0441\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0441 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u043c \u0438\u043c\u0435\u043d\u0435\u043c, \u043d\u043e \u0441 \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u0441\u0438\u0433\u043d\u0430\u0442\u0443\u0440\u0430\u043c\u0438. \u041f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043a\u0430 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u0430 \u0432 Java \u0438 C++, \u043e\u043d\u0430 \u0447\u0430\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">class Foo {     public static void main(String[] args) {         System.out.println(Hello());     }      public static String Hello() {         return Hello(\"world\");     }      public static String Hello(String name) {         return \"Hello, \" + name;     } }<\/code><\/pre>\n<p>  <\/p>\n<p>Python \u043d\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u043f\u043e\u0438\u0441\u043a \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u043f\u043e \u0441\u0438\u0433\u043d\u0430\u0442\u0443\u0440\u0435, \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e \u0438\u043c\u0435\u043d\u0438. \u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043a\u043e\u0434, \u044f\u0432\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0430\u043d\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u0442\u0438\u043f\u044b \u0438 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432, \u043d\u043e \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u043d\u0435\u0443\u043a\u043b\u044e\u0436\u0435, \u0438 \u0442\u0430\u043a\u043e\u0439 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0438 \u043b\u0443\u0447\u0448\u0435 \u0438\u0437\u0431\u0435\u0433\u0430\u0442\u044c:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">def quadrilateral_area(*args):     if len(args) == 4:         quadrilateral = Quadrilateral(*args)     elif len(args) == 1:         quadrilateral = args[0]     else:         raise TypeError()      return quadrilateral.area()<\/code><\/pre>\n<p>  <\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u0430\u043c \u043d\u0443\u0436\u043d\u044b type hints, \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435\u0441\u044c \u043c\u043e\u0434\u0443\u043b\u0435\u043c <code>typing<\/code> \u0441 \u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440\u043e\u043c <code>@overload<\/code>:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">from typing import overload  @overload def quadrilateral_area(     q: Quadrilateral ) -&gt; float: ...  @overload def quadrilateral_area(     p1: Point, p2: Point,     p3: Point, p4: Point ) -&gt; float: ...<\/code><\/pre>\n<p>  <\/p>\n<h2 id=\"avtovivifikaciya\">\u0410\u0432\u0442\u043e\u0432\u0438\u0432\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f<\/h2>\n<p>  <\/p>\n<p><code>collections.defaultdict<\/code> \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u043b\u043e\u0432\u0430\u0440\u044c, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e, \u0435\u0441\u043b\u0438 \u0437\u0430\u043f\u0440\u043e\u0448\u0435\u043d\u043d\u044b\u0439 \u043a\u043b\u044e\u0447 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442 (\u0432\u043c\u0435\u0441\u0442\u043e \u0432\u044b\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u043d\u0438\u044f <code>KeyError<\/code>). \u0414\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f <code>defaultdict<\/code>\u0432\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u043d\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u0434\u0435\u0444\u043e\u043b\u0442\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u0430 \u0444\u0430\u0431\u0440\u0438\u043a\u0443 \u0442\u0430\u043a\u0438\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439.<\/p>\n<p>  <\/p>\n<p>\u0422\u0430\u043a \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u043b\u043e\u0432\u0430\u0440\u044c \u0441 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u043e \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u044b\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0445 \u0441\u043b\u043e\u0432\u0430\u0440\u0435\u0439, \u0447\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438 \u0432\u0440\u043e\u0434\u0435 <code>d[a][b][c]...[z]<\/code>.<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">&gt;&gt;&gt; def infinite_dict(): ...     return defaultdict(infinite_dict) ... &gt;&gt;&gt; d = infinite_dict() &gt;&gt;&gt; d[1][2][3][4] = 10 &gt;&gt;&gt; dict(d[1][2][3][5]) {}<\/code><\/pre>\n<p>  <\/p>\n<p>\u0422\u0430\u043a\u043e\u0435 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u00ab\u0430\u0432\u0442\u043e\u0432\u0438\u0432\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0435\u0439\u00bb, \u044d\u0442\u043e\u0442 \u0442\u0435\u0440\u043c\u0438\u043d \u043f\u0440\u0438\u0448\u0451\u043b \u0438\u0437 Perl.<\/p>\n<p>  <\/p>\n<h2 id=\"instancirovanie\">\u0418\u043d\u0441\u0442\u0430\u043d\u0446\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435<\/h2>\n<p>  <\/p>\n<p>\u0418\u043d\u0441\u0442\u0430\u043d\u0446\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0432 \u0441\u0435\u0431\u044f \u0434\u0432\u0430 \u0432\u0430\u0436\u043d\u044b\u0445 \u0448\u0430\u0433\u0430. \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0438\u0437 \u043a\u043b\u0430\u0441\u0441\u0430 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043c\u0435\u0442\u043e\u0434 <code>__new__<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0437\u0434\u0430\u0451\u0442 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043d\u043e\u0432\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442. \u0417\u0430\u0442\u0435\u043c \u0438\u0437 \u043d\u0435\u0433\u043e Python \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u043c\u0435\u0442\u043e\u0434 <code>__init__<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0437\u0430\u0434\u0430\u0451\u0442 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u044d\u0442\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430.<\/p>\n<p>  <\/p>\n<p>\u041e\u0434\u043d\u0430\u043a\u043e <code>__init__<\/code> \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u0432\u0430\u043d, \u0435\u0441\u043b\u0438 <code>__new__<\/code> \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442, \u043d\u0435 \u044f\u0432\u043b\u044f\u044e\u0449\u0438\u0439\u0441\u044f \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u043e\u043c \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043e\u0431\u044a\u0435\u043a\u0442 \u043c\u043e\u0433 \u0431\u044b\u0442\u044c \u0441\u043e\u0437\u0434\u0430\u043d \u0434\u0440\u0443\u0433\u0438\u043c \u043a\u043b\u0430\u0441\u0441\u043e\u043c, \u0438 \u0437\u043d\u0430\u0447\u0438\u0442 <code>__init__<\/code> \u0443\u0436\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u043b\u0441\u044f \u043d\u0430 \u043e\u0431\u044a\u0435\u043a\u0442\u0435:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">class Foo:     def __new__(cls, x):         return dict(x=x)      def __init__(self, x):         print(x)  # Never called  print(Foo(0))<\/code><\/pre>\n<p>  <\/p>\n<p>\u042d\u0442\u043e \u0442\u0430\u043a\u0436\u0435 \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u043d\u0435 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u044b \u0442\u043e\u0433\u043e \u0436\u0435 \u043a\u043b\u0430\u0441\u0441\u0430 \u0432 <code>__new__<\/code> \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u0431\u044b\u0447\u043d\u043e\u0433\u043e \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430 (<code>Foo(...)<\/code>). \u042d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u043a \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u043c\u0443 \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044e <code>__init__<\/code>, \u0438\u043b\u0438 \u0434\u0430\u0436\u0435 \u043a \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u043e\u0439 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438.<\/p>\n<p>  <\/p>\n<p>\u0411\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u0430\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044f:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">class Foo:     def __new__(cls, x):         return Foo(-x)  # Recursion<\/code><\/pre>\n<p>  <\/p>\n<p>\u0414\u0432\u043e\u0439\u043d\u043e\u0435 \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 <code>__init__<\/code>:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">class Foo:     def __new__(cls, x):         if x &lt; 0:             return Foo(-x)         return super().__new__(cls)      def __init__(self, x):         print(x)         self._x = x<\/code><\/pre>\n<p>  <\/p>\n<p>\u041f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">class Foo:     def __new__(cls, x):         if x &lt; 0:             return cls.__new__(cls, -x)         return super().__new__(cls)      def __init__(self, x):         print(x)         self._x = x<\/code><\/pre>\n<p>  <\/p>\n<h2 id=\"operator--i-srezy\">\u041e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 [] \u0438 \u0441\u0440\u0435\u0437\u044b<\/h2>\n<p>  <\/p>\n<p>\u0412 Python \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 <code>[]<\/code>, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0432 \u043c\u0430\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043c\u0435\u0442\u043e\u0434 <code>__getitem__<\/code>. \u0422\u0430\u043a, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u043e \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">class Cycle:     def __init__(self, lst):         self._lst = lst      def __getitem__(self, index):         return self._lst[             index % len(self._lst)         ]  print(Cycle(['a', 'b', 'c'])[100])  # 'b'<\/code><\/pre>\n<p>  <\/p>\n<p>\u041d\u0435\u043e\u0431\u044b\u0447\u043d\u043e\u0435 \u0437\u0434\u0435\u0441\u044c \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 <code>[]<\/code> \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441. \u0421 \u0435\u0433\u043e \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e <code>[2]<\/code>, \u043d\u043e \u0438 <code>[2:10]<\/code>, <code>[2:10:2]<\/code>, <code>[2::2]<\/code> \u0438 \u0434\u0430\u0436\u0435 <code>[:]<\/code>. \u0421\u0435\u043c\u0430\u043d\u0442\u0438\u043a\u0430 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430 \u0442\u0430\u043a\u0430\u044f: [start:stop:step], \u043e\u0434\u043d\u0430\u043a\u043e \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u043b\u044e\u0431\u044b\u043c \u0438\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432.<\/p>\n<p>  <\/p>\n<p>\u041d\u043e \u0435\u0441\u043b\u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u044d\u0442\u043e\u0433\u043e \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441\u0430 <code>__getitem__<\/code>, \u0447\u0442\u043e \u043e\u043d \u043f\u043e\u043b\u0443\u0447\u0438\u0442 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u043d\u043e\u0433\u043e \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430? \u0418\u043c\u0435\u043d\u043d\u043e \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442 slice-\u043e\u0431\u044a\u0435\u043a\u0442\u044b.<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">In : class Inspector: ...:     def __getitem__(self, index): ...:         print(index) ...: In : Inspector()[1] 1 In : Inspector()[1:2] slice(1, 2, None) In : Inspector()[1:2:3] slice(1, 2, 3) In : Inspector()[:] slice(None, None, None)<\/code><\/pre>\n<p>  <\/p>\n<p>\u041c\u043e\u0436\u043d\u043e \u0434\u0430\u0436\u0435 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441\u044b \u043a\u043e\u0440\u0442\u0435\u0436\u0435\u0439 \u0438 \u0441\u043b\u0430\u0439\u0441\u043e\u0432:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">In : Inspector()[:, 0, :] (slice(None, None, None), 0, slice(None, None, None))<\/code><\/pre>\n<p>  <\/p>\n<p><code>slice<\/code> \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0434\u0435\u043b\u0430\u0435\u0442, \u0442\u043e\u043b\u044c\u043a\u043e \u0445\u0440\u0430\u043d\u0438\u0442 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u044b <code>start<\/code>, <code>stop<\/code> \u0438 <code>step<\/code>.<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">In : s = slice(1, 2, 3) In : s.start Out: 1 In : s.stop Out: 2 In : s.step Out: 3<\/code><\/pre>\n<p>  <\/p>\n<h2 id=\"preryvanie-korutiny-asyncio\">\u041f\u0440\u0435\u0440\u044b\u0432\u0430\u043d\u0438\u0435 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b asyncio<\/h2>\n<p>  <\/p>\n<p>\u041b\u044e\u0431\u0443\u044e \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u0443\u044e \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0443 (coroutine) <code>asyncio<\/code> \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0435\u0440\u0432\u0430\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u0435\u0442\u043e\u0434\u0430 <code>cancel()<\/code>. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u0432 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0443 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0430 <code>CancelledError<\/code>, \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u044d\u0442\u0430 \u0438 \u0432\u0441\u0435 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0435 \u0441 \u043d\u0435\u0439 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u0431\u0443\u0434\u0443\u0442 \u043f\u0440\u0435\u0440\u0432\u0430\u043d\u044b, \u043f\u043e\u043a\u0430 \u043e\u0448\u0438\u0431\u043a\u0430 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u0439\u043c\u0430\u043d\u0430 \u0438 \u043f\u043e\u0434\u0430\u0432\u043b\u0435\u043d\u0430.<\/p>\n<p>  <\/p>\n<p><code>CancelledError<\/code> \u2014 \u043f\u043e\u0434\u043a\u043b\u0430\u0441\u0441 <code>Exception<\/code>, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442 \u0435\u0451 \u043c\u043e\u0436\u043d\u043e \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e \u043f\u043e\u0439\u043c\u0430\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u0438 <code>try ... except Exception<\/code>, \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u043d\u043e\u0439 \u0434\u043b\u044f \u043b\u043e\u0432\u043b\u0438 \u00ab\u043b\u044e\u0431\u044b\u0445 \u043e\u0448\u0438\u0431\u043e\u043a\u00bb. \u0427\u0442\u043e\u0431\u044b \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e \u0434\u043b\u044f \u0441\u043e\u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u043f\u043e\u0439\u043c\u0430\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0443, \u043f\u0440\u0438\u0434\u0451\u0442\u0441\u044f \u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">try:     await action() except asyncio.CancelledError:     raise except Exception:     logging.exception('action failed')<\/code><\/pre>\n<p>  <\/p>\n<h2 id=\"planirovanie-ispolneniya\">\u041f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f<\/h2>\n<p>  <\/p>\n<p>\u0414\u043b\u044f \u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043a\u0430\u043a\u043e\u0433\u043e-\u0442\u043e \u043a\u043e\u0434\u0430 \u0432 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u0432 <code>asyncio<\/code> \u043e\u0431\u044b\u0447\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u044e\u0442 task, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 <code>await asyncio.sleep(x)<\/code>:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">import asyncio  async def do(n=0):     print(n)     await asyncio.sleep(1)     loop.create_task(do(n + 1))     loop.create_task(do(n + 1))  loop = asyncio.get_event_loop() loop.create_task(do()) loop.run_forever()<\/code><\/pre>\n<p>  <\/p>\n<p>\u041d\u043e \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043d\u043e\u0432\u043e\u043e\u0433\u043e \u0442\u0430\u0441\u043a\u0430 \u043c\u043e\u0436\u0435\u0442 \u0441\u0442\u043e\u0438\u0442\u044c \u0434\u043e\u0440\u043e\u0433\u043e, \u0434\u0430 \u044d\u0442\u043e \u0438 \u043d\u0435 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0434\u0435\u043b\u0430\u0442\u044c, \u0435\u0441\u043b\u0438 \u0432\u044b \u043d\u0435 \u043f\u043b\u0430\u043d\u0438\u0440\u0443\u0435\u0442\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 (\u0432\u0440\u043e\u0434\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>do<\/code> \u0432 \u043c\u043e\u0451\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435). \u0412\u043c\u0435\u0441\u0442\u043e \u044d\u0442\u043e\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>loop.call_later<\/code> \u0438 <code>loop.call_at<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0437\u0430\u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u044b\u0437\u043e\u0432 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0433\u043e \u043a\u043e\u043b\u043b\u0431\u0435\u043a\u0430:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">import asyncio                       def do(n=0):                            print(n)                            loop = asyncio.get_event_loop()     loop.call_later(1, do, n+1)         loop.call_later(1, do, n+1)      loop = asyncio.get_event_loop()     do()                                loop.run_forever()<\/code><\/pre>\n<\/div>\n<p>        <script class=\"js-mediator-script\">!function(e){function t(t,n){if(!(n in e)){for(var r,a=e.document,i=a.scripts,o=i.length;o--;)if(-1!==i[o].src.indexOf(t)){r=i[o];break}if(!r){r=a.createElement(\"script\"),r.type=\"text\/javascript\",r.async=!0,r.defer=!0,r.src=t,r.charset=\"UTF-8\";var d=function(){var e=a.getElementsByTagName(\"script\")[0];e.parentNode.insertBefore(r,e)};\"[object Opera]\"==e.opera?a.addEventListener?a.addEventListener(\"DOMContentLoaded\",d,!1):e.attachEvent(\"onload\",d):d()}}}t(\"\/\/mediator.mail.ru\/script\/2820404\/\",\"_mediator\")}(window);<\/script>     <br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/post\/425125\/\"> https:\/\/habr.com\/post\/425125\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\">\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/79\/ur\/ug\/79urugmgbgzjhlpozccip51e5_s.png\"><\/p>\n<p>  <\/p>\n<p>\u042d\u0442\u043e \u0447\u0435\u0442\u0432\u0451\u0440\u0442\u0430\u044f \u043f\u043e\u0434\u0431\u043e\u0440\u043a\u0430 \u0441\u043e\u0432\u0435\u0442\u043e\u0432 \u043f\u0440\u043e Python \u0438 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438\u0437 \u043c\u043e\u0435\u0433\u043e \u0430\u0432\u0442\u043e\u0440\u0441\u043a\u043e\u0433\u043e \u043a\u0430\u043d\u0430\u043b\u0430 <a href=\"http:\/\/t.me\/pythonetc\">@pythonetc<\/a>.<\/p>\n<p>  <\/p>\n<p>\u041f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0435 \u043f\u043e\u0434\u0431\u043e\u0440\u043a\u0438:<\/p>\n<p>  <\/p>\n<ul>\n<li><a href=\"https:\/\/habr.com\/company\/mailru\/blog\/422789\/\">\u0410\u0432\u0433\u0443\u0441\u0442 2018<\/a><\/li>\n<li><a href=\"https:\/\/habr.com\/company\/mailru\/blog\/419025\/\">\u0418\u044e\u043b\u044c 2018<\/a><\/li>\n<li><a href=\"https:\/\/habr.com\/company\/mailru\/blog\/416605\/\">\u0418\u044e\u043d\u044c 2018<\/a><\/li>\n<\/ul>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-290257","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/290257","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=290257"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/290257\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=290257"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=290257"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=290257"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}