scope of function parameters

Steven D'Aprano steve+comp.lang.python at pearwood.info
Mon May 30 05:14:39 EDT 2011


On Mon, 30 May 2011 11:08:23 +0200, Laurent Claessens wrote:

> Le 30/05/2011 11:02, Terry Reedy a écrit :
>> On 5/30/2011 3:38 AM, Laurent wrote:
>>
>>>  Cool. I was thinking that "5" was the name, but
>>>   >>>  5.__add__(6)
>>>  File "<stdin>", line 1
>>>  5.__add__(6)
>>
>>
>> Try 5 .__add__(6)
> 
> What is the rationale behind the fact to add a space between "5" and
> ".__add__" ?
> Why does it work ?

Because . is an operator just like + * & etc.

>>> s = "hello world"
>>> s . upper ( )
'HELLO WORLD'


In the case of integer literals, you need the space, otherwise Python 
will parse 5. as a float:


>>> 5.
5.0
>>> 5.__add__
  File "<stdin>", line 1
    5.__add__
            ^
SyntaxError: invalid syntax
>>> 5 .__add__
<method-wrapper '__add__' of int object at 0x8ce3d60>





-- 
Steven



More information about the Python-list mailing list