Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alias a function in Python

Tags:

I would like a copy of the print function that is called debug. How can I alias a function in Python?

like image 785
Randomblue Avatar asked Jan 21 '13 14:01

Randomblue


1 Answers

You can simply assign debug = print in Python 3.

In Python 2 print isn't a function. There no way to give yourself a debug statement that works exactly like print (print 1,, print 1 >> sys.stderr etc.). Best you can do is write a wrapper around the print statement:

def debug(s):     print s 

You can also disable the print statement and use the Python 3 version:

from __future__ import print_function debug = print 

If you do this, you cannot use the statement version (print x) anymore. It's probably the way to go if you're not breaking any old code.

like image 179
Pavel Anossov Avatar answered Oct 11 '22 02:10

Pavel Anossov