{"id":298456,"date":"2020-02-10T15:00:11","date_gmt":"2020-02-10T15:00:11","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=298456"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=298456","title":{"rendered":"\u041f\u043e\u0434\u0431\u043e\u0440\u043a\u0430 @pythonetc, \u044f\u043d\u0432\u0430\u0440\u044c 2020"},"content":{"rendered":"\n<div class=\"post__text post__text-html\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/company\/mailru\/blog\/487728\/\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/7e\/0u\/p1\/7e0up1b_kwsv8ztw27ay70sxnxa.jpeg\"><\/p>\n<p>  \u041d\u043e\u0432\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 @pythonetc.<\/p>\n<p>  \u2190 <a href=\"https:\/\/habr.com\/ru\/search\/?q=pythonetc#h\">\u041f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0435 \u043f\u0443\u0431\u043b\u0438\u043a\u0430\u0446\u0438\u0438<\/a><\/p>\n<div style=\"text-align:center;\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/208\/1cc\/1fa\/2081cc1fa6dbebbbb6ddd4108512ff5b.png\"><\/div>\n<p>  \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u0431\u043b\u043e\u043a\u043e\u0432 <code>except<\/code> \u0438\u043c\u0435\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435: \u0435\u0441\u043b\u0438 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u043e\u0439\u043c\u0430\u043d\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c\u0438 \u0431\u043b\u043e\u043a\u0430\u043c\u0438, \u0442\u043e \u0435\u0433\u043e \u043f\u043e\u0439\u043c\u0430\u0435\u0442 \u0432\u0435\u0440\u0445\u043d\u0438\u0439 \u0431\u043b\u043e\u043a. \u042d\u0442\u043e\u0442 \u043a\u043e\u0434 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0442\u0430\u043a, \u043a\u0430\u043a \u0437\u0430\u0434\u0443\u043c\u0430\u043d\u043e:<br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<pre><code class=\"python\">import logging  def get(storage, key, default):     try:         return storage[key]     except LookupError:         return default     except IndexError:         return get(storage, 0, default)     except TypeError:         logging.exception('unsupported key')         return default  print(get([1], 0, 42))  # 1 print(get([1], 10, 42))  # 42 print(get([1], 'x', 42))  # error msg, 42 <\/code><\/pre>\n<p>  <code>except IndexError<\/code> \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e <code>IndexError<\/code> \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u043e\u0434\u043a\u043b\u0430\u0441\u0441\u043e\u043c <code>LookupError<\/code>. \u0411\u043e\u043b\u0435\u0435 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0435 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0432\u0441\u0435\u0433\u0434\u0430 \u0434\u043e\u043b\u0436\u043d\u043e \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u044c\u0441\u044f \u0432\u044b\u0448\u0435:<\/p>\n<pre><code class=\"python\">import logging  def get(storage, key, default):     try:     return storage[key]     except IndexError:     return get(storage, 0, default)     except LookupError:     return default     except TypeError:     logging.exception('unsupported key')     return default  print(get([1], 0, 42))  # 1 print(get([1], 10, 42))  # 1 print(get([1], 'x', 42))  # error msg, 42 <\/code><\/pre>\n<p>  <\/p>\n<div style=\"text-align:center;\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/208\/1cc\/1fa\/2081cc1fa6dbebbbb6ddd4108512ff5b.png\"><\/div>\n<p>  Python \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e\u0435 \u043f\u0440\u0438\u0441\u0432\u0430\u0438\u0432\u0430\u043d\u0438\u0435. \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u0432\u0441\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0438\u0437\u043c\u0435\u043d\u044f\u044e\u0442\u0441\u044f \u0441\u0440\u0430\u0437\u0443 \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u0445 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0439. \u0411\u043e\u043b\u0435\u0435 \u0442\u043e\u0433\u043e, \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043b\u044e\u0431\u043e\u0435 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435, \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u044e\u0449\u0435\u0435 \u043f\u0440\u0438\u0441\u0432\u0430\u0438\u0432\u0430\u043d\u0438\u0435, \u0430 \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435:<\/p>\n<pre><code class=\"python\">def shift_inplace(lst, k):     size = len(lst)     lst[k:], lst[0:k] = lst[0:-k], lst[-k:]  lst = list(range(10))  shift_inplace(lst, -3) print(lst) # [3, 4, 5, 6, 7, 8, 9, 0, 1, 2]  shift_inplace(lst, 5) print(lst) # [8, 9, 0, 1, 2, 3, 4, 5, 6, 7] <\/code><\/pre>\n<p>  <\/p>\n<div style=\"text-align:center;\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/208\/1cc\/1fa\/2081cc1fa6dbebbbb6ddd4108512ff5b.png\"><\/div>\n<p>  Python \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0441 \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c \u0447\u0438\u0441\u043b\u043e\u043c \u0432\u043c\u0435\u0441\u0442\u043e \u0432\u044b\u0447\u0438\u0442\u0430\u043d\u0438\u044f. \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"python\">class Velocity:     SPEED_OF_LIGHT = 299_792_458      def __init__(self, amount):         self.amount = amount      def __add__(self, other):         return type(self)(             (self.amount + other.amount) \/             (                 1 +                 self.amount * other.amount \/                 self.SPEED_OF_LIGHT ** 2             )         )      def __neg__(self):         return type(self)(-self.amount)      def __str__(self):         amount = int(self.amount)         return f'{amount} m\/s' <\/code><\/pre>\n<p>  \u042d\u0442\u043e\u0442 \u043a\u043e\u0434 \u043d\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442:<\/p>\n<pre><code class=\"python\">v1 = Velocity(20_000_000) v2 = Velocity(10_000_000)  print(v1 - v2) # TypeError: unsupported operand type(s) for -: 'Velocity' and 'Velocity  <\/code><\/pre>\n<p>  \u0417\u0430\u0431\u0430\u0432\u043d\u043e, \u043d\u043e \u044d\u0442\u043e\u0442 \u043a\u043e\u0434 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442:<\/p>\n<pre><code class=\"plaintext\">v1 = Velocity(20_000_000) v2 = Velocity(10_000_000)  print(v1 +- v2) # 10022302 m\/s <\/code><\/pre>\n<p>  <\/p>\n<div style=\"text-align:center;\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/208\/1cc\/1fa\/2081cc1fa6dbebbbb6ddd4108512ff5b.png\"><\/div>\n<p>  \u042d\u0442\u0430 \u0447\u0430\u0441\u0442\u044c \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0430 Telegram-\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u043c <a href=\"https:\/\/habr.com\/ru\/users\/orsinium\/\" class=\"user_link\">orsinium<\/a>.<\/p>\n<p>  \u0424\u0443\u043d\u043a\u0446\u0438\u044f \u043d\u0435 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u043c \u0438 \u043e\u0431\u044b\u0447\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439. \u0415\u0441\u043b\u0438 \u0432 \u0442\u0435\u043b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f <code>yield<\/code>, \u0442\u043e \u043e\u043d\u0430 \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440:<\/p>\n<pre><code class=\"python\">def zeros(*, count: int, lazy: bool):         if lazy:             for _ in range(count):                 yield 0             else:                 return [0] * count  zeros(count=10, lazy=True) # &lt;generator object zeros at 0x7ff0062f2a98&gt;  zeros(count=10, lazy=False) # &lt;generator object zeros at 0x7ff0073da570&gt;  list(zeros(count=10, lazy=False)) # [] <\/code><\/pre>\n<p>  \u041e\u0434\u043d\u0430\u043a\u043e \u043e\u0431\u044b\u0447\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043c\u043e\u0436\u0435\u0442 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0434\u0440\u0443\u0433\u043e\u0439 \u0438\u0442\u0435\u0440\u0430\u0442\u043e\u0440:<\/p>\n<pre><code class=\"python\">def _lazy_zeros(*, count: int):     for _ in range(count):         yield 0      def zeros(*, count: int, lazy: bool):     if lazy:         return _lazy_zeros(count=count)     return [0] * count  zeros(count=10, lazy=True) # &lt;generator object _lazy_zeros at 0x7ff0062f2750&gt;  zeros(count=10, lazy=False) # [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] <\/code><\/pre>\n<p>  \u0410 \u0442\u0430\u043a\u043e\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u043e\u043b\u0435\u0437\u0435\u043d \u0432 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u0441 \u043f\u0440\u043e\u0441\u0442\u044b\u043c\u0438 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f\u043c\u0438-\u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u0430\u043c\u0438:<\/p>\n<pre><code class=\"python\">def zeros(*, count: int, lazy: bool):     if lazy:         return (0 for _ in range(count))     return [0] * count <\/code><\/pre>\n<p>  <\/p>\n<div style=\"text-align:center;\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/208\/1cc\/1fa\/2081cc1fa6dbebbbb6ddd4108512ff5b.png\"><\/div>\n<p>  \u041f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 generator comprehension \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043a\u043e\u0431\u043a\u0438:<\/p>\n<pre><code class=\"python\">&gt;&gt;&gt; g = x**x for x in range(10)     File &quot;&lt;stdin&gt;&quot;, line 1         g = x**x for x in range(10)             ^ SyntaxError: invalid syntax &gt;&gt;&gt; g = (x**x for x in range(10)) &gt;&gt;&gt; g &lt;generator object &lt;genexpr&gt; at 0x7f90ed650258&gt;  <\/code><\/pre>\n<p>  \u041e\u0434\u043d\u0430\u043a\u043e \u0438\u0445 \u043c\u043e\u0436\u043d\u043e \u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c, \u0435\u0441\u043b\u0438 comprehension \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u0438:<\/p>\n<pre><code class=\"python\">&gt;&gt;&gt; list((x**x for x in range(4))) [1, 1, 4, 27] &gt;&gt;&gt; list(x**x for x in range(4)) [1, 1, 4, 27]  <\/code><\/pre>\n<p>  \u042d\u0442\u043e \u043d\u0435 \u0432\u0435\u0440\u043d\u043e \u0434\u043b\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u0443 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432:<\/p>\n<pre><code class=\"python\">&gt;&gt;&gt; print((x**x for x in range(4)), end='\\n') &lt;generator object &lt;genexpr&gt; at 0x7f90ed650468&gt; &gt;&gt;&gt; &gt;&gt;&gt; &gt;&gt;&gt; print(x**x for x in range(4), end='\\n')     File &quot;&lt;stdin&gt;&quot;, line 1 SyntaxError: Generator expression must be parenthesized if not sole argument <\/code><\/pre>\n<\/div>\n<p> \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\/ru\/company\/mailru\/blog\/487728\/\"> https:\/\/habr.com\/ru\/company\/mailru\/blog\/487728\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/company\/mailru\/blog\/487728\/\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/7e\/0u\/p1\/7e0up1b_kwsv8ztw27ay70sxnxa.jpeg\"><\/p>\n<p>  \u041d\u043e\u0432\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 @pythonetc.<\/p>\n<p>  \u2190 <a href=\"https:\/\/habr.com\/ru\/search\/?q=pythonetc#h\">\u041f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0435 \u043f\u0443\u0431\u043b\u0438\u043a\u0430\u0446\u0438\u0438<\/a><\/p>\n<div style=\"text-align:center;\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/208\/1cc\/1fa\/2081cc1fa6dbebbbb6ddd4108512ff5b.png\"><\/div>\n<p>  \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u0431\u043b\u043e\u043a\u043e\u0432 <code>except<\/code> \u0438\u043c\u0435\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435: \u0435\u0441\u043b\u0438 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u043e\u0439\u043c\u0430\u043d\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c\u0438 \u0431\u043b\u043e\u043a\u0430\u043c\u0438, \u0442\u043e \u0435\u0433\u043e \u043f\u043e\u0439\u043c\u0430\u0435\u0442 \u0432\u0435\u0440\u0445\u043d\u0438\u0439 \u0431\u043b\u043e\u043a. \u042d\u0442\u043e\u0442 \u043a\u043e\u0434 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0442\u0430\u043a, \u043a\u0430\u043a \u0437\u0430\u0434\u0443\u043c\u0430\u043d\u043e:  <\/p>\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-298456","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/298456","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=298456"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/298456\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=298456"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=298456"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=298456"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}