The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

import-bot (20211) [Avatar] Offline
#1
[Originally posted by dogberry]

Python newbie question ...

Sometimes, it might be useful to be able to define a simple command to e.g.
run a function with a commonly used combination of arguments. Is there a
mechanism in Python for defining something like the equivalent of a Unix
alias, other than using another function to do this, so that no parentheses
are necessary in the command (as they would be if you used another function to
do this)?

For example ...

def blt()
make_sandwich("bacon","lettuce","tomato")

is a nice shortcut for the make_sandwich function, but I still have to
remember the parentheses every time when I type blt(). Is there a way to defer
execution to have something like

blt = make_sandwich("bacon","lettuce","tomato") [I know this will not work]

Perhaps it done somehow using compile or exec?

Any help greatly appreciated

Gordon
import-bot (20211) [Avatar] Offline
#2
Re: Defining aliases
[Originally posted by d. harms]

Hi Gordon,

I can't think of a way to do this in Python alone. You might be able to get
this functionality through using something like the Emacs python shell along
with the Emacs abbrev-mode, but I don't have Emacs currently running on my
system to try this out.

> Python newbie question ...
>
> Sometimes, it might be useful to be able to define a simple command to e.g.
> run a function with a commonly used combination of arguments. Is there a
> mechanism in Python for defining something like the equivalent of a Unix
> alias, other than using another function to do this, so that no parentheses
> are necessary in the command (as they would be if you used another function to
> do this)?
>
> For example ...
>
> def blt()
> make_sandwich("bacon","lettuce","tomato"smilie
>
> is a nice shortcut for the make_sandwich function, but I still have to
> remember the parentheses every time when I type blt(). Is there a way to defer
> execution to have something like
>
> blt = make_sandwich("bacon","lettuce","tomato"smilie [I know this will not
work]
>
> Perhaps it done somehow using compile or exec?
>
> Any help greatly appreciated
>
> Gordon