default behavior

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Fri Aug 13 07:51:40 CEST 2010


On Thu, 12 Aug 2010 13:28:26 -0700, David Niergarth wrote:

> Peter Otten <__pete... at web.de> wrote:
>>
>> >>> 1 .conjugate()
>>
>>
> This is a syntax I never noticed before. My built-in complier (eyes)
> took one look and said: "that doesn't work." Has this always worked in
> Python but I never noticed?


Yes. Here is is working in Python 2.2:

[steve at sylar ~]$ python2.2
Python 2.2.3 (#1, Aug 12 2010, 01:08:27)
[GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 2 .__add__(3)
5


Syntactically, it also worked as far back as Python 1.5, although it is 
rather pointless since int objects didn't gain any methods until 2.2:

[steve at sylar ~]$ python1.5
Python 1.5.2 (#1, Apr  1 2009, 22:55:54)  [GCC 4.1.2 20070925 (Red Hat 
4.1.2-27)] on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> 2 .__add__(3)
Traceback (innermost last):
  File "<stdin>", line 1, in ?
AttributeError: 'int' object has no attribute '__add__'


> I see other instance examples also work.
> 
>   >>> '1' .zfill(2)
>   '01'

You don't need the space between strings and the attribute access: 
"1".zfill(2) is fine. You only need it for numbers, due to the ambiguity 
between the decimal point and dotted attribute access.




-- 
Steven



More information about the Python-list mailing list