{"id":294301,"date":"2019-09-05T15:00:16","date_gmt":"2019-09-05T15:00:16","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=294301"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=294301","title":{"rendered":"\u041f\u043e\u0434\u0431\u043e\u0440\u043a\u0430 @pythonetc, \u0430\u0432\u0433\u0443\u0441\u0442 2019"},"content":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/mg\/mb\/s3\/mgmbs3xjemuybjkqe11zobhdnyw.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=%5Bpythonetc%5D&amp;target_type=posts\">\u041f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0435 \u043f\u043e\u0434\u0431\u043e\u0440\u043a\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>  \u0415\u0441\u043b\u0438 \u0443 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 \u043a\u043b\u0430\u0441\u0441\u0430 \u043d\u0435\u0442 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0430 \u0441 \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u043c \u0438\u043c\u0435\u043d\u0435\u043c, \u0442\u043e \u043e\u043d \u043f\u044b\u0442\u0430\u0435\u0442\u0441\u044f \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c\u0441\u044f \u043a \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0443 \u043a\u043b\u0430\u0441\u0441\u0430 \u0441 \u0442\u0435\u043c \u0436\u0435 \u0438\u043c\u0435\u043d\u0435\u043c.<\/p>\n<pre><code class=\"python\">&gt;&gt;&gt; class A: ...     x = 2 ... &gt;&gt;&gt; A.x 2 &gt;&gt;&gt; A().x 2<\/code><\/pre>\n<p><a name=\"habracut\"><\/a><br \/>  \u042d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043b\u0435\u0433\u043a\u043e \u043c\u043e\u0436\u0435\u0442 \u0438\u043c\u0435\u0442\u044c \u0430\u0442\u0440\u0438\u0431\u0443\u0442, \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043d\u0435\u0442 \u0443 \u043a\u043b\u0430\u0441\u0441\u0430, \u0438\u043b\u0438 \u0438\u043c\u0435\u0442\u044c \u0430\u0442\u0440\u0438\u0431\u0443\u0442 \u0441 \u0434\u0440\u0443\u0433\u0438\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c:<\/p>\n<pre><code class=\"python\">&gt;&gt;&gt; class A: ...     x = 2 ...     def __init__(self): ...         self.x = 3 ...         self.y = 4 ... &gt;&gt;&gt; A().x 3 &gt;&gt;&gt; A.x 2 &gt;&gt;&gt; A().y 4 &gt;&gt;&gt; A.y AttributeError: type object 'A' has no attribute 'y'<\/code><\/pre>\n<p>  \u0415\u0441\u043b\u0438 \u0436\u0435 \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435, \u0447\u0442\u043e\u0431\u044b \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u0432\u0451\u043b \u0441\u0435\u0431\u044f \u0442\u0430\u043a, \u0441\u043b\u043e\u0432\u043d\u043e \u0443 \u043d\u0435\u0433\u043e \u043d\u0435\u0442 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0430, \u0445\u043e\u0442\u044f \u043e\u043d \u0435\u0441\u0442\u044c \u0443 \u043a\u043b\u0430\u0441\u0441\u0430, \u0442\u043e \u043f\u0440\u0438\u0434\u0451\u0442\u0441\u044f \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0439 \u0434\u0435\u0441\u043a\u0440\u0438\u043f\u0442\u043e\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0437\u0430\u043f\u0440\u0435\u0449\u0430\u0435\u0442 \u043e\u0431\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u0438\u0437 \u044d\u0442\u043e\u0433\u043e \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430:<\/p>\n<pre><code class=\"python\">class ClassOnlyDescriptor:     def __init__(self, value):         self._value = value         self._name = None  # see __set_name__      def __get__(self, instance, owner):         if instance is not None:             raise AttributeError(                 f'{instance} has no attribute {self._name}'             )          return self._value      def __set_name__(self, owner, name):         self._name = name   class_only = ClassOnlyDescriptor   class A:     x = class_only(2)   print(A.x)  # 2 A().x       # raises AttributeError <\/code><\/pre>\n<p>  \u0421\u043c. \u0442\u0430\u043a\u0436\u0435, \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 Django-\u0434\u0435\u043a\u043e\u0440\u0430\u0442\u043e\u0440 <code>classonlymethod<\/code>: <a href=\"https:\/\/github.com\/django\/django\/blob\/b709d701303b3877387020c1558a590713b09853\/django\/utils\/decorators.py#L6\">https:\/\/github.com\/django\/django\/blob\/b709d701303b3877387020c1558a590713b09853\/django\/utils\/decorators.py#L6<\/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>  \u0424\u0443\u043d\u043a\u0446\u0438\u044f\u043c, \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u043d\u044b\u043c \u0432 \u0442\u0435\u043b\u0435 \u043a\u043b\u0430\u0441\u0441\u0430, \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u044d\u0442\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 \u043d\u0435\u0434\u043e\u0441\u0442\u0443\u043f\u043d\u0430. \u042d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u043d\u043e \u043f\u043e\u0442\u043e\u043c\u0443, \u0447\u0442\u043e \u044d\u0442\u0430 \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0445\u043e\u0434\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043a\u043b\u0430\u0441\u0441\u0430.<\/p>\n<pre><code class=\"python\">&gt;&gt;&gt; class A: ...     x = 2 ...     def f(): ...         print(x) ...     f() ... [...] NameError: name 'x' is not defined<\/code><\/pre>\n<p>  \u041e\u0431\u044b\u0447\u043d\u043e \u044d\u0442\u043e \u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u043e\u0439: \u043c\u0435\u0442\u043e\u0434\u044b \u043e\u0431\u044a\u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u043b\u0430\u0441\u0441\u0430 \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0441\u0442\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u0430\u043c\u0438 \u0438 \u0431\u044b\u0442\u044c \u0432\u044b\u0437\u0432\u0430\u043d\u043d\u044b\u043c\u0438 \u043f\u043e\u0437\u0434\u043d\u0435\u0435:<\/p>\n<pre><code class=\"python\">&gt;&gt;&gt; class A: ...     x = 2 ...     def f(self): ...         print(self.x) ... &gt;&gt;&gt; &gt;&gt;&gt; &gt;&gt;&gt; A().f() 2<\/code><\/pre>\n<p>  \u0423\u0434\u0438\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e, \u043d\u043e \u0442\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0435 \u0432\u0435\u0440\u043d\u043e \u0438 \u0434\u043b\u044f comprehensions. \u0423 \u043d\u0438\u0445 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0435 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0438 \u043e\u043d\u0438 \u0442\u043e\u0436\u0435 \u043d\u0435 \u0438\u043c\u0435\u044e\u0442 \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u043e\u0431\u043b\u0430\u0441\u0442\u044f\u043c \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u043a\u043b\u0430\u0441\u0441\u043e\u0432. \u042d\u0442\u043e \u043e\u0447\u0435\u043d\u044c \u043b\u043e\u0433\u0438\u0447\u043d\u043e \u0441 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f generator comprehensions: \u043a\u043e\u0434 \u0432 \u043d\u0438\u0445 \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 \u043a\u043b\u0430\u0441\u0441 \u0443\u0436\u0435 \u0441\u043e\u0437\u0434\u0430\u043d.<\/p>\n<pre><code class=\"python\">&gt;&gt;&gt; class A: ...     x = 2 ...     y = [x for _ in range(5)] ... [...] NameError: name 'x' is not defined<\/code><\/pre>\n<p>  \u041e\u0434\u043d\u0430\u043a\u043e \u0443 comprehensions \u043d\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a <code>self<\/code>. \u0415\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a <\/p>\n<pre><code class=\"python\">x<\/code><\/pre>\n<p> \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0438 \u0435\u0449\u0451 \u043e\u0434\u043d\u043e\u0439 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438 (\u0430\u0433\u0430, \u0434\u0443\u0440\u0430\u0446\u043a\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435):<\/p>\n<pre><code class=\"python\">&gt;&gt;&gt; class A: ...     x = 2 ...     y = (lambda x=x: [x for _ in range(5)])() ... &gt;&gt;&gt; A.y [2, 2, 2, 2, 2] <\/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>  \u0412 Python <code>None<\/code> \u044d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u043d\u043e <code>None<\/code>, \u0442\u0430\u043a \u0447\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f, \u0447\u0442\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u043d\u0430 <code>None<\/code> \u043c\u043e\u0436\u043d\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>==<\/code>:<\/p>\n<pre><code class=\"python\">ES_TAILS = ('s', 'x', 'z', 'ch', 'sh')   def make_plural(word, exceptions=None):     if exceptions == None:  # \u2190 \u2190 \u2190         exceptions = {}      if word in exceptions:         return exceptions[word]     elif any(word.endswith(t) for t in ES_TAILS):         return word + 'es'     elif word.endswith('y'):         return word[0:-1] + 'ies'     else:         return word + 's'  exceptions = dict(     mouse='mice', )  print(make_plural('python')) print(make_plural('bash')) print(make_plural('ruby')) print(make_plural('mouse', exceptions=exceptions)) <\/code><\/pre>\n<p>  \u041d\u043e \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u043e\u0439. \u0414\u0430, <code>None<\/code> \u0440\u0430\u0432\u043d\u043e <code>None<\/code>, \u043d\u043e \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u043d\u043e. \u041f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0442\u043e\u0436\u0435 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u0440\u0430\u0432\u043d\u044b <code>None<\/code>:<\/p>\n<pre><code class=\"python\">&gt;&gt;&gt; class A: ...     def __eq__(self, other): ...             return True ... &gt;&gt;&gt; A() == None True &gt;&gt;&gt; A() is None False<\/code><\/pre>\n<p>  \u0415\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f \u0441 <code>None<\/code> \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 <code>is None<\/code>.<\/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>  \u0427\u0438\u0441\u043b\u0430 \u0441 \u043f\u043b\u0430\u0432\u0430\u044e\u0449\u0435\u0439 \u0437\u0430\u043f\u044f\u0442\u043e\u0439 \u0432 Python \u043c\u043e\u0433\u0443\u0442 \u0438\u043c\u0435\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f NaN. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0442\u0430\u043a\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>math.nan<\/code>. <code>nan<\/code> \u043d\u0435 \u0440\u0430\u0432\u043d\u043e \u043d\u0438\u0447\u0435\u043c\u0443, \u0432\u043a\u043b\u044e\u0447\u0430\u044f \u0441\u0435\u0431\u044f:<\/p>\n<pre><code class=\"python\">&gt;&gt;&gt; math.nan == math.nan False<\/code><\/pre>\n<p>  \u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, NaN-\u043e\u0431\u044a\u0435\u043a\u0442 \u043d\u0435 \u0443\u043d\u0438\u043a\u0430\u043b\u0435\u043d, \u0443 \u0432\u0430\u0441 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0437\u043d\u044b\u0445 NaN-\u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0438\u0437 \u0440\u0430\u0437\u043d\u044b\u0445 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u043e\u0432:<\/p>\n<pre><code class=\"python\">&gt;&gt;&gt; float('nan') nan &gt;&gt;&gt; float('nan') is float('nan') False<\/code><\/pre>\n<p>  \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e, \u0432 \u0446\u0435\u043b\u043e\u043c, \u0432\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c NaN \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043a\u043b\u044e\u0447\u0430 \u0441\u043b\u043e\u0432\u0430\u0440\u044f:<\/p>\n<pre><code class=\"python\">&gt;&gt;&gt; d = {} &gt;&gt;&gt; d[float('nan')] = 1 &gt;&gt;&gt; d[float('nan')] = 2 &gt;&gt;&gt; d {nan: 1, nan: 2} <\/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>  <code>typing<\/code> \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u0442\u0438\u043f\u044b \u0434\u043b\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u0432. \u0414\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043c\u043e\u0436\u043d\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c, \u043a\u0430\u043a\u043e\u0439 \u0442\u0438\u043f \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442\u0441\u044f, \u043a\u0430\u043a\u043e\u0439 \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u0442\u0441\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u0443 \u0438 \u043a\u0430\u043a\u043e\u0439 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <\/p>\n<pre><code class=\"python\">return<\/code><\/pre>\n<p>. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <code>Generator[int, None, bool]<\/code> \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u0446\u0435\u043b\u044b\u0435 \u0447\u0438\u0441\u043b\u0430, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0431\u0443\u043b\u0435\u0432\u044b \u0438 \u043d\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 <code>g.send()<\/code>.<\/p>\n<p>  \u0410 \u0432\u043e\u0442 \u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u043e\u0441\u043b\u043e\u0436\u043d\u0435\u0435. <code>chain_while<\/code> \u043f\u0440\u043e\u043a\u0441\u0438\u0440\u0443\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u043e\u0442 \u0434\u0440\u0443\u0433\u0438\u0445 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u0432 \u0434\u043e \u0442\u0435\u0445 \u043f\u043e\u0440, \u043f\u043e\u043a\u0430 \u043e\u0434\u0438\u043d \u0438\u0437 \u043d\u0438\u0445 \u043d\u0435 \u0432\u0435\u0440\u043d\u0451\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0438\u0433\u043d\u0430\u043b\u043e\u043c \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0438 \u0441 \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439 <code>condition<\/code>:<\/p>\n<pre><code class=\"python\">from typing import Generator, Callable, Iterable, TypeVar  Y = TypeVar('Y') S = TypeVar('S') R = TypeVar('R')   def chain_while(     iterables: Iterable[Generator[Y, S, R]],     condition: Callable[[R], bool], ) -&gt; Generator[Y, S, None]:     for it in iterables:         result = yield from it         if not condition(result):             break   def r(x: int) -&gt; Generator[int, None, bool]:     yield from range(x)     return x % 2 == 1   print(list(chain_while(     [         r(5),         r(4),         r(3),     ],     lambda x: x is True, ))) <\/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>  \u0417\u0430\u0434\u0430\u0442\u044c \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438 \u0434\u043b\u044f \u0444\u0430\u0431\u0440\u0438\u0447\u043d\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430 \u043d\u0435 \u0442\u0430\u043a \u043f\u0440\u043e\u0441\u0442\u043e, \u043a\u0430\u043a \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f. \u0421\u0440\u0430\u0437\u0443 \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u0435\u0447\u0442\u043e \u043f\u043e\u0434\u043e\u0431\u043d\u043e\u0435:<\/p>\n<pre><code class=\"python\">class A:     @classmethod     def create(cls) -&gt; 'A':         return cls()<\/code><\/pre>\n<p>  \u041d\u043e \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u043d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e. \u0425\u0438\u0442\u0440\u043e\u0441\u0442\u044c \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e <code>create<\/code> \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043d\u0435 <code>A<\/code>, \u043e\u043d \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 <code>cls<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f <code>A<\/code> <i>\u0438\u043b\u0438<\/i> \u043e\u0434\u043d\u0438\u043c \u0438\u0437 \u0435\u0433\u043e \u043f\u043e\u0442\u043e\u043c\u043a\u043e\u0432. \u0412\u0437\u0433\u043b\u044f\u043d\u0438\u0442\u0435 \u043d\u0430 \u043a\u043e\u0434:<\/p>\n<pre><code class=\"python\">class A:     @classmethod     def create(cls) -&gt; 'A':         return cls()   class B(A):     @classmethod     def create(cls) -&gt; 'B':         return super().create()<\/code><\/pre>\n<p>  \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 mypy \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0448\u0438\u0431\u043a\u0430 <code>error: Incompatible return value type (got \"A\", expected \"B\")<\/code>. \u041f\u043e\u0432\u0442\u043e\u0440\u044e\u0441\u044c, \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e <code>super().create()<\/code> \u0430\u043d\u043d\u043e\u0442\u0438\u0440\u043e\u0432\u0430\u043d \u043a\u0430\u043a \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0438\u0439 <code>A<\/code>, \u0445\u043e\u0442\u044f \u0432 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043e\u043d \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 <code>B<\/code>.<\/p>\n<p>  \u042d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c, \u0435\u0441\u043b\u0438 \u0430\u043d\u043d\u043e\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c <code>cls<\/code> \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>TypeVar<\/code>:<\/p>\n<pre><code class=\"python\">AType = TypeVar('AType') BType = TypeVar('BType')   class A:     @classmethod     def create(cls: Type[AType]) -&gt; AType:         return cls()   class B(A):     @classmethod     def create(cls: Type[BType]) -&gt; BType:         return super().create()<\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c <code>create<\/code> \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043a\u043b\u0430\u0441\u0441\u0430 <code>cls<\/code>. \u041e\u0434\u043d\u0430\u043a\u043e \u044d\u0442\u0438 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0440\u0430\u0441\u043f\u043b\u044b\u0432\u0447\u0430\u0442\u044b, \u043c\u044b \u043f\u043e\u0442\u0435\u0440\u044f\u043b\u0438 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u0442\u043e\u043c, \u0447\u0442\u043e <code>cls<\/code> \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u043e\u0434\u0442\u0438\u043f\u043e\u043c <code>A<\/code>:<\/p>\n<pre><code class=\"python\">AType = TypeVar('AType')   class A:     DATA = 42     @classmethod     def create(cls: Type[AType]) -&gt; AType:         print(cls.DATA)         return cls()<\/code><\/pre>\n<p>  \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043e\u0448\u0438\u0431\u043a\u0443 <code>\"Type[AType]\" has no attribute \"DATA\"<\/code>.<\/p>\n<p>  \u0427\u0442\u043e\u0431\u044b \u0435\u0451 \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c, \u043d\u0443\u0436\u043d\u043e \u044f\u0432\u043d\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c <code>AType<\/code> \u043a\u0430\u043a \u043f\u043e\u0434\u0442\u0438\u043f <code>A<\/code>. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f <code>TypeVar<\/code> \u0441 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u043c <code>bound<\/code>.<\/p>\n<pre><code class=\"python\">AType = TypeVar('AType', bound='A') BType = TypeVar('BType', bound='B')   class A:     DATA = 42     @classmethod     def create(cls: Type[AType]) -&gt; AType:         print(cls.DATA)         return cls()   class B(A):     @classmethod     def create(cls: Type[BType]) -&gt; BType:         return super().create() <\/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\/ru\/company\/mailru\/blog\/466315\/\"> https:\/\/habr.com\/ru\/company\/mailru\/blog\/466315\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/mg\/mb\/s3\/mgmbs3xjemuybjkqe11zobhdnyw.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=%5Bpythonetc%5D&amp;target_type=posts\">\u041f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0435 \u043f\u043e\u0434\u0431\u043e\u0440\u043a\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>  \u0415\u0441\u043b\u0438 \u0443 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 \u043a\u043b\u0430\u0441\u0441\u0430 \u043d\u0435\u0442 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0430 \u0441 \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u043c \u0438\u043c\u0435\u043d\u0435\u043c, \u0442\u043e \u043e\u043d \u043f\u044b\u0442\u0430\u0435\u0442\u0441\u044f \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c\u0441\u044f \u043a \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0443 \u043a\u043b\u0430\u0441\u0441\u0430 \u0441 \u0442\u0435\u043c \u0436\u0435 \u0438\u043c\u0435\u043d\u0435\u043c.<\/p>\n<pre><code class=\"python\">&gt;&gt;&gt; class A: ...     x = 2 ... &gt;&gt;&gt; A.x 2 &gt;&gt;&gt; A().x 2<\/code><\/pre>\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-294301","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/294301","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=294301"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/294301\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=294301"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=294301"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=294301"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}