<i id='zPCI7'><tr id='zPCI7'><dt id='zPCI7'><q id='zPCI7'><span id='zPCI7'><b id='zPCI7'><form id='zPCI7'><ins id='zPCI7'></ins><ul id='zPCI7'></ul><sub id='zPCI7'></sub></form><legend id='zPCI7'></legend><bdo id='zPCI7'><pre id='zPCI7'><center id='zPCI7'></center></pre></bdo></b><th id='zPCI7'></th></span></q></dt></tr></i><div id='zPCI7'><tfoot id='zPCI7'></tfoot><dl id='zPCI7'><fieldset id='zPCI7'></fieldset></dl></div>

    <small id='zPCI7'></small><noframes id='zPCI7'>

          <bdo id='zPCI7'></bdo><ul id='zPCI7'></ul>

        <tfoot id='zPCI7'></tfoot><legend id='zPCI7'><style id='zPCI7'><dir id='zPCI7'><q id='zPCI7'></q></dir></style></legend>
      1. 访问文字上的属性适用于所有类型,但不适用于 `int`;为什么?

        时间:2024-04-20
        1. <i id='1cT1N'><tr id='1cT1N'><dt id='1cT1N'><q id='1cT1N'><span id='1cT1N'><b id='1cT1N'><form id='1cT1N'><ins id='1cT1N'></ins><ul id='1cT1N'></ul><sub id='1cT1N'></sub></form><legend id='1cT1N'></legend><bdo id='1cT1N'><pre id='1cT1N'><center id='1cT1N'></center></pre></bdo></b><th id='1cT1N'></th></span></q></dt></tr></i><div id='1cT1N'><tfoot id='1cT1N'></tfoot><dl id='1cT1N'><fieldset id='1cT1N'></fieldset></dl></div>

        2. <small id='1cT1N'></small><noframes id='1cT1N'>

          <tfoot id='1cT1N'></tfoot>
            <bdo id='1cT1N'></bdo><ul id='1cT1N'></ul>

                <tbody id='1cT1N'></tbody>

                • <legend id='1cT1N'><style id='1cT1N'><dir id='1cT1N'><q id='1cT1N'></q></dir></style></legend>
                  本文介绍了访问文字上的属性适用于所有类型,但不适用于 `int`;为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  I have read that everything in python is an object, and as such I started to experiment with different types and invoking __str__ on them — at first I was feeling really excited, but then I got confused.

                  >>> "hello world".__str__()
                  'hello world'
                  >>> [].__str__()
                  '[]'
                  >>> 3.14.__str__()
                  '3.14'
                  >>> 3..__str__()
                  '3.0'
                  >>> 123.__str__()
                    File "<stdin>", line 1
                      123.__str__()
                                ^
                  SyntaxError: invalid syntax
                  

                  • Why does something.__str__() work for "everything" besides int?
                  • Is 123 not an object of type int?

                  解决方案

                  So you think you can  dance  floating-point?

                  123 is just as much of an object as 3.14, the "problem" lies within the grammar rules of the language; the parser thinks we are about to define a float — not an int with a trailing method call.

                  We will get the expected behavior if we wrap the number in parenthesis, as in the below.

                  >>> (123).__str__()
                  '123'

                  Or if we simply add some whitespace after 123:

                  >>> 123 .__str__()
                  '123'


                  The reason it does not work for 123.__str__() is that the dot following the 123 is interpreted as the decimal-point of some partially declared floating-point.

                  >>> 123.__str__()
                    File "", line 1
                      123.__str__()
                                ^
                  SyntaxError: invalid syntax

                  The parser tries to interpret __str__() as a sequence of digits, but obviously fails — and we get a SyntaxError basically saying that the parser stumbled upon something that it did not expect.



                  Elaboration

                  When looking at 123.__str__() the python parser could use either 3 characters and interpret these 3 characters as an integer, or it could use 4 characters and interpret these as the start of a floating-point.

                  123.__str__()
                  ^^^ - int
                  

                  123.__str__()
                  ^^^^- start of floating-point
                  

                  Just as a little child would like as much cake as possible on their plate, the parser is greedy and would like to swallow as much as it can all at once — even if this isn't always the best of ideas —as such the latter ("better") alternative is chosen.

                  When it later realizes that __str__() can in no way be interpreted as the decimals of a floating-point it is already too late; SyntaxError.

                  Note

                   123 .__str__() # works fine
                  

                  In the above snippet, 123  (note the space) must be interpreted as an integer since no number can contain spaces. This means that it is semantically equivalent to (123).__str__().

                  Note

                   123..__str__() # works fine
                  

                  The above also works because a number can contain at most one decimal-point, meaning that it is equivalent to (123.).__str__().



                  For the language-lawyers

                  This section contains the lexical definition of the relevant literals.

                  Lexical analysis - 2.4.5 Floating point literals

                  floatnumber   ::=  pointfloat | exponentfloat
                  pointfloat    ::=  [intpart] fraction | intpart "."
                  exponentfloat ::=  (intpart | pointfloat) exponent
                  intpart       ::=  digit+
                  fraction      ::=  "." digit+
                  exponent      ::=  ("e" | "E") ["+" | "-"] digit+
                  

                  Lexical analysis - 2.4.4 Integer literals

                  integer        ::=  decimalinteger | octinteger | hexinteger | bininteger
                  decimalinteger ::=  nonzerodigit digit* | "0"+
                  nonzerodigit   ::=  "1"..."9"
                  digit          ::=  "0"..."9"
                  octinteger     ::=  "0" ("o" | "O") octdigit+
                  hexinteger     ::=  "0" ("x" | "X") hexdigit+
                  bininteger     ::=  "0" ("b" | "B") bindigit+
                  octdigit       ::=  "0"..."7"
                  hexdigit       ::=  digit | "a"..."f" | "A"..."F"
                  bindigit       ::=  "0" | "1"
                  

                  这篇关于访问文字上的属性适用于所有类型,但不适用于 `int`;为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何处理用yfinance下载的多级列名 下一篇:证书验证失败:无法获取本地颁发者证书

                  相关文章

                  <small id='Xpm89'></small><noframes id='Xpm89'>

                  • <bdo id='Xpm89'></bdo><ul id='Xpm89'></ul>

                • <i id='Xpm89'><tr id='Xpm89'><dt id='Xpm89'><q id='Xpm89'><span id='Xpm89'><b id='Xpm89'><form id='Xpm89'><ins id='Xpm89'></ins><ul id='Xpm89'></ul><sub id='Xpm89'></sub></form><legend id='Xpm89'></legend><bdo id='Xpm89'><pre id='Xpm89'><center id='Xpm89'></center></pre></bdo></b><th id='Xpm89'></th></span></q></dt></tr></i><div id='Xpm89'><tfoot id='Xpm89'></tfoot><dl id='Xpm89'><fieldset id='Xpm89'></fieldset></dl></div>

                  <legend id='Xpm89'><style id='Xpm89'><dir id='Xpm89'><q id='Xpm89'></q></dir></style></legend>
                  1. <tfoot id='Xpm89'></tfoot>