Help me understand this

Jean-Paul Calderone exarkun at divmod.com
Tue Jan 30 12:52:34 EST 2007


On Tue, 30 Jan 2007 14:39:28 -0300, Gabriel Genellina <gagsl-py at yahoo.com.ar> wrote:
>En Tue, 30 Jan 2007 06:34:01 -0300, Beej <beej at beej.us> escribió:
>
>> But here's one I still don't get:
>>
>>>>> type(2)
>> <type 'int'>
>>>>> type((2))
>> <type 'int'>
>>>>> (2).__add__(1)
>> 3
>>>>> 2.__add__(1)
>>   File "<stdin>", line 1
>>     2.__add__(1)
>>             ^
>> SyntaxError: invalid syntax
>
>It appears to be a bug, either in the grammar implementation, or in the
>grammar documentation.
>These are the relevant rules:
>
>attributeref ::= primary "." identifier
>
>primary ::= atom | attributeref | subscription | slicing | call
>
>atom ::= identifier | literal | enclosure
>
>literal ::= stringliteral | integer | longinteger | floatnumber |
>imagnumber
>
>An integer is a primary so 2.__add(1) should be valid.

A float is, too.  2.__add is a float followed by an identifier.
Not legal.  As pointed out elsewhere in the thread, (2). forces
it to be an integer followed by a ".".

Jean-Paul



More information about the Python-list mailing list