Set command alias to print in python?

In bash, you can give the alias command, for example:

alias e=echoset alias e="echo blah" 

I want to know how to do the same in Python. I know that you can give class aliases, but when I try to give a command (for example, a print expression) an alias, I get an error:

 >>> p = print File "<stdin>", line 1 p = print ^ SyntaxError: invalid syntax 

I can do it:

 p = "print" exec(p) 

But this is not the same as smoothing, and I can not give any input to the command.

Update: @atzz You guessed it wasn’t like to print. I am trying to find a job:

The intention is to install the command, but instead it just beeps when I enter this:
>>> beep = Popen(['play', '-q', '/home/Username/Mich/Sound Effects/Beeps/beep-17-short.ogg'])

Then, when I enter a beep into the tooltip, it shows this:
>>> beep <subprocess.Popen object at 0x9967b8c>

But besides that, I have this problem, at least now I know that you cannot give aliases to operators.

+4
source share
4 answers

To answer your new question, if you want to do this and sound the beep later:

 beep = Popen(['play', '-q', '/home/Username/Mich/Sound Effects/Beeps/beep-17-short.ogg']) 

Change it to:

 beep = lambda: Popen(['play', '-q', '/home/Username/Mich/Sound Effects/Beeps/beep-17-short.ogg']) 

Or just a function :-)

 def beep(): Popen(['play', '-q', '/home/Username/Mich/Sound Effects/Beeps/beep-17-short.ogg']) 

Otherwise, "beep" will simply hold the return value of the Popen.

+2
source

Is your question specific to print ?

In Python before 3.0, print is a keyword in the grammar of a language. Since it is not a first-class language object, you cannot assign it to a variable.

In Python 3.0, the print keyword is missing; there instead of the print function ( documentation ).

Using the appropriate operator of the future, you can use the print function in Python 2.6 +:

 from __future__ import print_function a = print a("Hello!") 
+7
source

This is only the print statement that you come across, because it is an operator (for example, while / if / etc.), And not a function.

If you want to "rename" (for example) len, everything will work fine.

+6
source

This is exactly what happened with Python 3. PY3 print has a function, not a keyword, and yes, you can do it there.

+1
source

All Articles