Set command alias for print in python?
Asked Answered
G

4

7

In bash you can give a command an alias like so:

alias e=echoset 
alias e="echo blah"

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

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

I can do this:

p = "print"
exec(p)

But that isn't really the same thing as aliasing, and I can't give any input to the command.

Update: @atzz You guessed right, it is not specific to print. What I am trying to get to work is this:

This is supposed to set 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 beep into the prompt, it shows this:
>>> beep <subprocess.Popen object at 0x9967b8c>

But other then this problem I have, at least now I know that you can't give statements aliases.

Grouse answered 30/8, 2012 at 6:47 Comment(0)
E
3

To answer your new question, if you want to do this and have beep execute 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 just hold the return value of Popen.

Elsieelsinore answered 3/9, 2012 at 21:20 Comment(0)
W
10

Is your question specific to print?

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

In Python 3.0, there is no keyword print; there's a print function instead (documentation).

With an appropriate future statement, you can use the print function in Python 2.6+:

from __future__ import print_function
a = print
a("Hello!")
Witten answered 30/8, 2012 at 6:54 Comment(1)
@gnibbler - Thanks for the correction. I "knew" it appeared in 2.7 so I even missed the contrary info in the doc I linked to :)Witten
E
7

It's only the print statement you'll have this problem with, because it's a statement (like while/if/etc.) rather than a function.

If you wanted to 'rename' (for instance) len, it would work just fine.

Elsieelsinore answered 30/8, 2012 at 6:51 Comment(0)
E
3

To answer your new question, if you want to do this and have beep execute 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 just hold the return value of Popen.

Elsieelsinore answered 3/9, 2012 at 21:20 Comment(0)
I
2

This behavior is exactly why Python 3 took place. In PY3 print is a function, not keyword, and yes, you can do it there.

Ijssel answered 30/8, 2012 at 6:51 Comment(2)
I don't think this is the reason why Py3 took place, but the rest is correct :-)Cho
Indeed :-) That is one of the reasons.Ijssel

© 2022 - 2024 — McMap. All rights reserved.