python 3's adoption

Lie Ryan lie.1296 at gmail.com
Thu Jan 28 11:54:26 EST 2010


On 01/28/10 20:12, Alf P. Steinbach wrote:
> * Steven D'Aprano:
>> On Wed, 27 Jan 2010 18:29:25 +0100, Alf P. Steinbach wrote:>
>> Instead of:
>>
>> print >>fileObj, x, y, z
>>
>> you use regular function syntax with a meaningful keyword:
>>
>> print(x, y, z, file=fileObj)
>>
>> If you want suppress the newline at the end of each print:
>>
>> print x, y, z,  # note the final comma
>>
>> compared to:
>>
>> print(x, y, z, end='')
> 
> Actually I thought the final comma thing was nice. It was like Basic. I
> think the 2.x 'print' must have been modeled on Basic's 'print'.

if that was true, then python missed the final semicolon

>> If you want to change the space between elements, instead of:
>>
>> sys.stdout.write(str(x) + "*" + str(y) + "*" + str(z) + '\n')
>>
>> you use:
>>
>> print(x, y, z, sep='*')
>>
>>
>> If you want to override the behaviour of print in a module, instead of
>> having to edit the source code of the module (which might not even be
>> available), all you need to do is monkey-patch it:
>>
>> import module
>> module.print = myprint
> 
>   >>> import builtins
>   >>>
>   >>> org_print = print
>   >>> builtins.print = 666
>   >>>
>   >>> print( "trallala" )
>   Traceback (most recent call last):
>     File "<stdin>", line 1, in <module>
>   TypeError: 'int' object is not callable
>   >>> org_print( "but is that really so smart?" )
>   but is that really so smart?
>   >>> _

Monkey patching follows (or should follow) the same rule as class
inheritance overriding: the overrider's input domain must be a superset
of the overriden's input domain and the overrider's output range must be
a subset of the overriden's output range. 666 object (int) is not even
remotely compatible with function object.



More information about the Python-list mailing list