Is there a way to globally set the print function separator?

John Black jblack at nopam.com
Mon Oct 9 17:04:36 EDT 2017


In article <org8pq$fgm$1 at gioia.aioe.org>, python at example.invalid says...
> 
> Le 09/10/2017 à 18:22, John Black a écrit :
> > I want sep="" to be the default without having to specify it every time I
> > call print.  Is that possible?
> 
>  >>> oldprint = print
>  >>> def print(*args,**kwargs):
> ...   oldprint(*args,**kwargs,sep='')
> ...
>  >>> print(1,2,3)
> 123

Winner!  Thanks all.

I want to make sure I understand what this line is doing: 

> oldprint = print

Experimenting, I find this is not a rename because I can use both 
function names.  It looks it literally copies the function "print" to 
another function called "oldprint".  But now, I have a way to modify the 
builtin funciton "print" by referencing oldprint.  Without oldprint, I 
have no way to directly modify print?  For example, based on your post, I 
tried:

def print(*args, **kw):
    print(*args, sep='', **kw)

meaning print calls print (itself) with sep=''.  But this failed and I 
guess the reason is that it would keep calling itself recursively adding 
sep='' each time?  Thanks.

John Black



More information about the Python-list mailing list