{"id":354137,"date":"2024-05-20T22:42:21","date_gmt":"2024-05-20T22:42:21","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=354137"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=354137","title":{"rendered":"<span>\u041d\u0435\u043c\u043d\u043e\u0436\u043a\u043e \u043e \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u0441\u0442\u0440\u043e\u043a \u0432 \u043f\u0438\u0442\u043e\u043d\u0435<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041a\u0430\u043a \u0432\u0441\u0435\u043c \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e, \u0432 \u043f\u0438\u0442\u043e\u043d\u0435 \u0435\u0441\u0442\u044c \u0447\u0435\u0442\u044b\u0440\u0435 \u0441\u043f\u043e\u0441\u043e\u0431\u0430 \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0441\u0442\u0440\u043e\u043a:<\/p>\n<ol>\n<li>\n<p>string.Template<\/p>\n<\/li>\n<li>\n<p>\u0441\u0438\u0448\u043d\u044b\u0439 \u0441\u0442\u0438\u043b\u044c<\/p>\n<\/li>\n<li>\n<p>f-\u0441\u0442\u0440\u043e\u043a\u0438<\/p>\n<\/li>\n<li>\n<p>str.format()<\/p>\n<\/li>\n<\/ol>\n<p>\u041f\u0435\u0440\u0432\u044b\u0439, <code>string.Template<\/code>, \u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u0440\u0430\u0437 \u0432\u0438\u0434\u0435\u043b \u0432 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445 \u043d\u0430 \u0432\u0435\u0440\u0441\u0438\u0438 \u043f\u0438\u0442\u043e\u043d\u0430 \u043d\u0435 \u0442\u043e 2.4, \u043d\u0435 \u0442\u043e 2.5. \u041e\u0447\u0435\u043d\u044c \u0434\u0430\u0432\u043d\u043e.<\/p>\n<p>\u0421\u0438\u0448\u043d\u044b\u0439 \u0441\u0442\u0438\u043b\u044c, \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435, \u0441\u0430\u043c\u044b\u0439 \u043d\u044f\u0448\u043d\u044b\u0439. \u041a \u0442\u043e\u043c\u0443 \u0436\u0435 \u0432 \u043f\u0438\u0442\u043e\u043d\u0435 \u043c\u043e\u0436\u043d\u043e \u0434\u0430\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430\u043c \u0438\u043c\u0435\u043d\u0430 \u0438 \u043f\u043e\u0441\u043b\u0435 % \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c <code>dict<\/code> \u0432\u043c\u0435\u0441\u0442\u043e <code>tuple<\/code> \u0438\u043b\u0438 \u043e\u0434\u0438\u043d\u043e\u0447\u043d\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u0435\u0441\u043b\u0438 \u043e\u043d\u043e \u043d\u0435 tuple. \u042d\u0442\u043e\u0442 \u0441\u0442\u0438\u043b\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0432 \u043c\u043e\u0434\u0443\u043b\u0435 <code>logging<\/code> \u2014 \u0441\u043c. \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043a\u043b\u0430\u0441\u0441\u0430 <code>LogRecord<\/code>: <em>The main information passed in is in msg and args, which are combined using str(msg) % args to create the message field of the record.<\/em> \u2014 \u0432\u044b \u043d\u0430\u0432\u0435\u0440\u043d\u044f\u043a\u0430 \u0432\u0435\u0437\u0434\u0435 \u0432\u0438\u0434\u0435\u043b\u0438 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0430\u0446\u0438\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0442\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0435 \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 <code>log.debug('%s, %s', 'hello', 'world)<\/code> \u0432\u043c\u0435\u0441\u0442\u043e <code>log.debug('%s, %s' % ('hello', 'world))<\/code>. \u041d\u043e \u043c\u043e\u0436\u043d\u043e \u0438 \u0442\u0430\u043a: <code>log.debug('%(greet)s, %(me)s', dict(greet='hello', me='world))<\/code><\/p>\n<p>f-\u0441\u0442\u0440\u043e\u043a\u0438 \u044d\u0442\u043e \u043d\u0435 \u0441\u043e\u0432\u0441\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435, \u044d\u0442\u043e \u0447\u0430\u0441\u0442\u044c \u043a\u043e\u0434\u0430 \u0438 \u0434\u043b\u044f \u0438\u0445 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0437\u0430\u0434\u0435\u0439\u0441\u0442\u0432\u0443\u0435\u0442\u0441\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440. \u041d\u043e \u043e\u043d\u0438 \u0441\u0434\u0435\u043b\u0430\u043d\u044b \u043f\u043e \u043e\u0431\u0440\u0430\u0437\u0443 \u0438 \u043f\u043e\u0434\u043e\u0431\u0438\u044e <code>str.format()<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u043f\u0438\u0441\u0430\u043d \u0432 <a href=\"https:\/\/peps.python.org\/pep-3101\/\" rel=\"noopener noreferrer nofollow\"><u>PEP 3101 \u2013 Advanced String Formatting<\/u><\/a>.<\/p>\n<p>\u0413\u0440\u0430\u043c\u043c\u0430\u0442\u0438\u043a\u0438 <a href=\"https:\/\/docs.python.org\/3\/library\/string.html#formatspec\" rel=\"noopener noreferrer nofollow\">str.format()<\/a> \u0438 <a href=\"https:\/\/docs.python.org\/3\/reference\/lexical_analysis.html#f-strings\" rel=\"noopener noreferrer nofollow\">f-\u0441\u0442\u0440\u043e\u043a<\/a> \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u0445\u043e\u0436\u0438, \u043d\u043e \u0434\u043b\u044f <code>str.format()<\/code> \u043e\u043d\u0430 \u043d\u0435\u043f\u043e\u043b\u043d\u0430\u044f \u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0430\u0440\u0441\u0435\u0440\u0430 \u0441\u043b\u0435\u0433\u043a\u0430 \u0443\u043f\u0440\u043e\u0449\u0451\u043d\u043d\u0430\u044f \u2014 \u0432 \u0441\u0438\u0448\u043d\u043e\u043c \u043c\u043e\u0434\u0443\u043b\u0435 <code>_string<\/code> \u0432\u0438\u0434\u0438\u043c \u043f\u0440\u043e\u0441\u0442\u043e\u0435 \u0441\u043a\u0430\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u043f\u043e\u0434\u0441\u0447\u0451\u0442 \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u044e\u0449\u0438\u0445 \u0438 \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u044e\u0449\u0438\u0445 \u0441\u043a\u043e\u0431\u043e\u043a. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u043e\u0436\u0435\u043c \u043d\u0430\u0431\u043b\u044e\u0434\u0430\u0442\u044c \u0432\u043e\u0442 \u0442\u0430\u043a\u043e\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0435 \u043e\u0442\u043b\u0438\u0447\u0438\u0435:<\/p>\n<pre><code class=\"python\">class MyObject:      def __format__(self, format_spec):         print(format_spec)         return \"Here we go.\"  print(f\"{MyObject():Format spec with {{ braces }} }\")<\/code><\/pre>\n<p>\u0432\u044b\u0434\u0430\u0451\u0442 \u043e\u0448\u0438\u0431\u043a\u0443<\/p>\n<pre><code>NameError: name 'braces' is not defined<\/code><\/pre>\n<p>\u041f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e <code>{ braces }<\/code> \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043a\u0430\u043a <code>set<\/code>, \u0441\u043e\u0441\u0442\u043e\u044f\u0449\u0438\u0439 \u0438\u0437 \u043e\u0434\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 <code>braces<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u043d\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u043b\u0438. \u041d\u043e <code>str.format()<\/code> \u043e\u0442\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u0431\u0435\u0437 \u043e\u0448\u0438\u0431\u043e\u043a:<\/p>\n<pre><code class=\"python\">print('{my_obj:Format spec with {{ braces }} }'.format(my_obj=MyObject()))<\/code><\/pre>\n<pre><code>Format spec with { braces } Here we go.<\/code><\/pre>\n<p>\u041f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0434\u0432\u043e\u0439\u043d\u044b\u0435 \u0441\u043a\u043e\u0431\u043a\u0438 \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u043a\u0430\u043a \u044d\u043a\u0440\u0430\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043e\u0434\u0438\u043d\u043e\u0447\u043d\u044b\u0445. \u041c\u043e\u0436\u043d\u043e \u0441\u0447\u0438\u0442\u0430\u0442\u044c \u044d\u0442\u043e \u0444\u0438\u0447\u0435\u0439.<\/p>\n<p>\u0418\u0442\u0430\u043a, <code>format_spec<\/code> \u0434\u043e\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e, \u043d\u043e \u043e\u043d\u0430 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0434\u0432\u043e\u0439\u043a\u043e\u0439:<\/p>\n<pre><code class=\"python\">x=1 y=2 z=3 print('x={x:{y:{z}}}'.format_map(locals()))<\/code><\/pre>\n<pre><code>ValueError: Max string recursion exceeded<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0430\u044f \u043d\u0435\u0433\u043b\u0443\u0431\u043e\u043a\u0430\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044f \u0441\u043b\u0443\u0436\u0438\u0442 \u0438\u0441\u043a\u043b\u044e\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0440\u0430\u0434\u0438 \u0433\u0438\u0431\u043a\u043e\u0441\u0442\u0438 \u0437\u0430\u0434\u0430\u043d\u0438\u044f \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u043d\u043e \u0434\u043b\u044f \u043b\u044e\u0434\u0435\u0439 \u0441 \u043a\u0440\u0435\u0430\u0442\u0438\u0432\u043d\u043e\u0439 \u0444\u0430\u043d\u0442\u0430\u0437\u0438\u0435\u0439 \u044d\u0442\u043e \u043d\u0435\u0441\u043e\u043c\u043d\u0435\u043d\u043d\u043e \u0441\u0435\u0440\u044c\u0451\u0437\u043d\u043e\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435.<\/p>\n<p>\u041a\u0430\u043a \u0432\u0438\u0434\u0438\u043c, <code>format_spec<\/code> \u043c\u043e\u0436\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0447\u0442\u043e \u0443\u0433\u043e\u0434\u043d\u043e, \u043d\u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0435 \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043e \u0432 \u043a\u043b\u0430\u0441\u0441\u0435 <code>Formatter<\/code> \u043c\u043e\u0434\u0443\u043b\u044f <code>string<\/code>. \u041e\u0434\u043d\u0430\u043a\u043e \u043d\u0438\u0447\u0442\u043e \u043d\u0435 \u043c\u0435\u0448\u0430\u0435\u0442 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u0432\u043e\u0439 formatter. \u0417\u0430\u0447\u0435\u043c? \u2014 \u041d\u0443, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u0439\u0442\u0438 \u0438\u043c\u0435\u043d\u0430 \u0432\u0441\u0435\u0445 \u043f\u043e\u0434\u0441\u0442\u0430\u043d\u043e\u0432\u043e\u043a \u0432 \u0441\u0442\u0440\u043e\u043a\u0435:<\/p>\n<pre><code class=\"python\">import string import _string  class SubstCollector(string.Formatter):      def format(self, format_string, \/, *args, **kwargs):         '''         Return names of all substitutions in format_string.         '''         self._substs = set()         self.vformat(format_string, args, kwargs)         return self._substs      def get_field(self, field_name, args, kwargs):         '''         Collect substitution names         i.e. `arg_name` according to         https:\/\/docs.python.org\/3\/library\/string.html#formatspec.         '''         first, rest = _string.formatter_field_name_split(field_name)         self._substs.add(first)         return '', first      def convert_field(self, value, conversion):         '''         Do nothing.         '''         return ''      def format_field(self, value, format_spec):         '''         Do nothing.         '''         return ''  print(SubstCollector().format(     'a={a[s].d:Format spec with b={b.c[d]} and {{braces}}} plus {e}' ))<\/code><\/pre>\n<pre><code>{'a', 'e', 'b'}<\/code><\/pre>\n<p>\u0418\u043b\u0438 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u043e\u0439 \u043e\u0442\u0441\u0442\u0443\u043f\u043e\u0432. \u0418\u043b\u0438, \u0447\u0442\u043e \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u043f\u0440\u0438\u043a\u043e\u043b\u044c\u043d\u0435\u0435, \u043d\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e \u0438 \u0437\u0430\u043c\u0443\u0442\u0438\u0442\u044c \u0441\u0432\u043e\u0439 \u043c\u0438\u043d\u0438-\u044f\u0437\u044b\u043a. \u041d\u043e \u044d\u0442\u043e \u044f \u0432\u0441\u0435\u0446\u0435\u043b\u043e \u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e \u043d\u0430 \u0432\u0430\u0448\u0435 \u0443\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u0438\u0435.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><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\/articles\/759250\/\"> https:\/\/habr.com\/ru\/articles\/759250\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041a\u0430\u043a \u0432\u0441\u0435\u043c \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e, \u0432 \u043f\u0438\u0442\u043e\u043d\u0435 \u0435\u0441\u0442\u044c \u0447\u0435\u0442\u044b\u0440\u0435 \u0441\u043f\u043e\u0441\u043e\u0431\u0430 \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0441\u0442\u0440\u043e\u043a:<\/p>\n<ol>\n<li>\n<p>string.Template<\/p>\n<\/li>\n<li>\n<p>\u0441\u0438\u0448\u043d\u044b\u0439 \u0441\u0442\u0438\u043b\u044c<\/p>\n<\/li>\n<li>\n<p>f-\u0441\u0442\u0440\u043e\u043a\u0438<\/p>\n<\/li>\n<li>\n<p>str.format()<\/p>\n<\/li>\n<\/ol>\n<p>\u041f\u0435\u0440\u0432\u044b\u0439, <code>string.Template<\/code>, \u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u0440\u0430\u0437 \u0432\u0438\u0434\u0435\u043b \u0432 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445 \u043d\u0430 \u0432\u0435\u0440\u0441\u0438\u0438 \u043f\u0438\u0442\u043e\u043d\u0430 \u043d\u0435 \u0442\u043e 2.4, \u043d\u0435 \u0442\u043e 2.5. \u041e\u0447\u0435\u043d\u044c \u0434\u0430\u0432\u043d\u043e.<\/p>\n<p>\u0421\u0438\u0448\u043d\u044b\u0439 \u0441\u0442\u0438\u043b\u044c, \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435, \u0441\u0430\u043c\u044b\u0439 \u043d\u044f\u0448\u043d\u044b\u0439. \u041a \u0442\u043e\u043c\u0443 \u0436\u0435 \u0432 \u043f\u0438\u0442\u043e\u043d\u0435 \u043c\u043e\u0436\u043d\u043e \u0434\u0430\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430\u043c \u0438\u043c\u0435\u043d\u0430 \u0438 \u043f\u043e\u0441\u043b\u0435 % \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c <code>dict<\/code> \u0432\u043c\u0435\u0441\u0442\u043e <code>tuple<\/code> \u0438\u043b\u0438 \u043e\u0434\u0438\u043d\u043e\u0447\u043d\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u0435\u0441\u043b\u0438 \u043e\u043d\u043e \u043d\u0435 tuple. \u042d\u0442\u043e\u0442 \u0441\u0442\u0438\u043b\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0432 \u043c\u043e\u0434\u0443\u043b\u0435 <code>logging<\/code> \u2014 \u0441\u043c. \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043a\u043b\u0430\u0441\u0441\u0430 <code>LogRecord<\/code>: <em>The main information passed in is in msg and args, which are combined using str(msg) % args to create the message field of the record.<\/em> \u2014 \u0432\u044b \u043d\u0430\u0432\u0435\u0440\u043d\u044f\u043a\u0430 \u0432\u0435\u0437\u0434\u0435 \u0432\u0438\u0434\u0435\u043b\u0438 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0430\u0446\u0438\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0442\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0435 \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 <code>log.debug('%s, %s', 'hello', 'world)<\/code> \u0432\u043c\u0435\u0441\u0442\u043e <code>log.debug('%s, %s' % ('hello', 'world))<\/code>. \u041d\u043e \u043c\u043e\u0436\u043d\u043e \u0438 \u0442\u0430\u043a: <code>log.debug('%(greet)s, %(me)s', dict(greet='hello', me='world))<\/code><\/p>\n<p>f-\u0441\u0442\u0440\u043e\u043a\u0438 \u044d\u0442\u043e \u043d\u0435 \u0441\u043e\u0432\u0441\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435, \u044d\u0442\u043e \u0447\u0430\u0441\u0442\u044c \u043a\u043e\u0434\u0430 \u0438 \u0434\u043b\u044f \u0438\u0445 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0437\u0430\u0434\u0435\u0439\u0441\u0442\u0432\u0443\u0435\u0442\u0441\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440. \u041d\u043e \u043e\u043d\u0438 \u0441\u0434\u0435\u043b\u0430\u043d\u044b \u043f\u043e \u043e\u0431\u0440\u0430\u0437\u0443 \u0438 \u043f\u043e\u0434\u043e\u0431\u0438\u044e <code>str.format()<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u043f\u0438\u0441\u0430\u043d \u0432 <a href=\"https:\/\/peps.python.org\/pep-3101\/\" rel=\"noopener noreferrer nofollow\"><u>PEP 3101 \u2013 Advanced String Formatting<\/u><\/a>.<\/p>\n<p>\u0413\u0440\u0430\u043c\u043c\u0430\u0442\u0438\u043a\u0438 <a href=\"https:\/\/docs.python.org\/3\/library\/string.html#formatspec\" rel=\"noopener noreferrer nofollow\">str.format()<\/a> \u0438 <a href=\"https:\/\/docs.python.org\/3\/reference\/lexical_analysis.html#f-strings\" rel=\"noopener noreferrer nofollow\">f-\u0441\u0442\u0440\u043e\u043a<\/a> \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u0445\u043e\u0436\u0438, \u043d\u043e \u0434\u043b\u044f <code>str.format()<\/code> \u043e\u043d\u0430 \u043d\u0435\u043f\u043e\u043b\u043d\u0430\u044f \u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0430\u0440\u0441\u0435\u0440\u0430 \u0441\u043b\u0435\u0433\u043a\u0430 \u0443\u043f\u0440\u043e\u0449\u0451\u043d\u043d\u0430\u044f \u2014 \u0432 \u0441\u0438\u0448\u043d\u043e\u043c \u043c\u043e\u0434\u0443\u043b\u0435 <code>_string<\/code> \u0432\u0438\u0434\u0438\u043c \u043f\u0440\u043e\u0441\u0442\u043e\u0435 \u0441\u043a\u0430\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u043f\u043e\u0434\u0441\u0447\u0451\u0442 \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u044e\u0449\u0438\u0445 \u0438 \u0437\u0430\u043a\u0440\u044b\u0432\u0430\u044e\u0449\u0438\u0445 \u0441\u043a\u043e\u0431\u043e\u043a. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u043e\u0436\u0435\u043c \u043d\u0430\u0431\u043b\u044e\u0434\u0430\u0442\u044c \u0432\u043e\u0442 \u0442\u0430\u043a\u043e\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0435 \u043e\u0442\u043b\u0438\u0447\u0438\u0435:<\/p>\n<pre><code class=\"python\">class MyObject:      def __format__(self, format_spec):         print(format_spec)         return \"Here we go.\"  print(f\"{MyObject():Format spec with {{ braces }} }\")<\/code><\/pre>\n<p>\u0432\u044b\u0434\u0430\u0451\u0442 \u043e\u0448\u0438\u0431\u043a\u0443<\/p>\n<pre><code>NameError: name 'braces' is not defined<\/code><\/pre>\n<p>\u041f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e <code>{ braces }<\/code> \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043a\u0430\u043a <code>set<\/code>, \u0441\u043e\u0441\u0442\u043e\u044f\u0449\u0438\u0439 \u0438\u0437 \u043e\u0434\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 <code>braces<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u043d\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u043b\u0438. \u041d\u043e <code>str.format()<\/code> \u043e\u0442\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u0431\u0435\u0437 \u043e\u0448\u0438\u0431\u043e\u043a:<\/p>\n<pre><code class=\"python\">print('{my_obj:Format spec with {{ braces }} }'.format(my_obj=MyObject()))<\/code><\/pre>\n<pre><code>Format spec with { braces } Here we go.<\/code><\/pre>\n<p>\u041f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0434\u0432\u043e\u0439\u043d\u044b\u0435 \u0441\u043a\u043e\u0431\u043a\u0438 \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u043a\u0430\u043a \u044d\u043a\u0440\u0430\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043e\u0434\u0438\u043d\u043e\u0447\u043d\u044b\u0445. \u041c\u043e\u0436\u043d\u043e \u0441\u0447\u0438\u0442\u0430\u0442\u044c \u044d\u0442\u043e \u0444\u0438\u0447\u0435\u0439.<\/p>\n<p>\u0418\u0442\u0430\u043a, <code>format_spec<\/code> \u0434\u043e\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e, \u043d\u043e \u043e\u043d\u0430 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0434\u0432\u043e\u0439\u043a\u043e\u0439:<\/p>\n<pre><code class=\"python\">x=1 y=2 z=3 print('x={x:{y:{z}}}'.format_map(locals()))<\/code><\/pre>\n<pre><code>ValueError: Max string recursion exceeded<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0430\u044f \u043d\u0435\u0433\u043b\u0443\u0431\u043e\u043a\u0430\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044f \u0441\u043b\u0443\u0436\u0438\u0442 \u0438\u0441\u043a\u043b\u044e\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0440\u0430\u0434\u0438 \u0433\u0438\u0431\u043a\u043e\u0441\u0442\u0438 \u0437\u0430\u0434\u0430\u043d\u0438\u044f \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u043d\u043e \u0434\u043b\u044f \u043b\u044e\u0434\u0435\u0439 \u0441 \u043a\u0440\u0435\u0430\u0442\u0438\u0432\u043d\u043e\u0439 \u0444\u0430\u043d\u0442\u0430\u0437\u0438\u0435\u0439 \u044d\u0442\u043e \u043d\u0435\u0441\u043e\u043c\u043d\u0435\u043d\u043d\u043e \u0441\u0435\u0440\u044c\u0451\u0437\u043d\u043e\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435.<\/p>\n<p>\u041a\u0430\u043a \u0432\u0438\u0434\u0438\u043c, <code>format_spec<\/code> \u043c\u043e\u0436\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0447\u0442\u043e \u0443\u0433\u043e\u0434\u043d\u043e, \u043d\u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0435 \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043e \u0432 \u043a\u043b\u0430\u0441\u0441\u0435 <code>Formatter<\/code> \u043c\u043e\u0434\u0443\u043b\u044f <code>string<\/code>. \u041e\u0434\u043d\u0430\u043a\u043e \u043d\u0438\u0447\u0442\u043e \u043d\u0435 \u043c\u0435\u0448\u0430\u0435\u0442 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u0432\u043e\u0439 formatter. \u0417\u0430\u0447\u0435\u043c? \u2014 \u041d\u0443, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u0439\u0442\u0438 \u0438\u043c\u0435\u043d\u0430 \u0432\u0441\u0435\u0445 \u043f\u043e\u0434\u0441\u0442\u0430\u043d\u043e\u0432\u043e\u043a \u0432 \u0441\u0442\u0440\u043e\u043a\u0435:<\/p>\n<pre><code class=\"python\">import string import _string  class SubstCollector(string.Formatter):      def format(self, format_string, \/, *args, **kwargs):         '''         Return names of all substitutions in format_string.         '''         self._substs = set()         self.vformat(format_string, args, kwargs)         return self._substs      def get_field(self, field_name, args, kwargs):         '''         Collect substitution names         i.e. `arg_name` according to         https:\/\/docs.python.org\/3\/library\/string.html#formatspec.         '''         first, rest = _string.formatter_field_name_split(field_name)         self._substs.add(first)         return '', first      def convert_field(self, value, conversion):         '''         Do nothing.         '''         return ''      def format_field(self, value, format_spec):         '''         Do nothing.         '''         return ''  print(SubstCollector().format(     'a={a[s].d:Format spec with b={b.c[d]} and {{braces}}} plus {e}' ))<\/code><\/pre>\n<pre><code>{'a', 'e', 'b'}<\/code><\/pre>\n<p>\u0418\u043b\u0438 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u043e\u0439 \u043e\u0442\u0441\u0442\u0443\u043f\u043e\u0432. \u0418\u043b\u0438, \u0447\u0442\u043e \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u043f\u0440\u0438\u043a\u043e\u043b\u044c\u043d\u0435\u0435, \u043d\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e \u0438 \u0437\u0430\u043c\u0443\u0442\u0438\u0442\u044c \u0441\u0432\u043e\u0439 \u043c\u0438\u043d\u0438-\u044f\u0437\u044b\u043a. \u041d\u043e \u044d\u0442\u043e \u044f \u0432\u0441\u0435\u0446\u0435\u043b\u043e \u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e \u043d\u0430 \u0432\u0430\u0448\u0435 \u0443\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u0438\u0435.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><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\/articles\/759250\/\"> https:\/\/habr.com\/ru\/articles\/759250\/<\/a><br \/><\/br><\/br><\/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-354137","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/354137","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=354137"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/354137\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=354137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=354137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=354137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}