[IPython-dev] Macro redesign

Robert Kern robert.kern at gmail.com
Thu May 3 17:52:32 EDT 2007

Ville M. Vainio wrote:
> Some post-0.8.2 brainstorming:
> I am not sure I like the current approach to macros. They have a
> "special" role Prompts.cachedoutput (if output is a macro, it gets
> called):
>            if isinstance(arg,Macro):
>                 print 'Executing Macro...'
>                 # in case the macro takes a long time to execute
>                 Term.cout.flush()
>                 self.shell.runlines(arg.value)
>                 return None
> I think Macro class should have __call__(m_arg), and get called
> normally via 'autocall' mechanism.
> Or we could add some extra code that always calls macros, even when
> autocall is off. IpyAutocallMixin subclass, perhaps?
> The rationale? Apart from abstract ideas like avoiding "special
> casing", I'd like to pass arguments to macros, and return values from
> them. This makes sense when you think how you'd normally write a small
> "script" - run some commands, then %edit the macro.
> You would access m_arg through itpl syntax ($m_arg), and return values
> through assigning to special variable called m_ret.
> This would allow using macros for many things you need to write
> functions, magics or aliases now.

That would pretty drastically change the semantics of macros. Macros are
currently simply executed in the interpreter's namespace. If you ultimately turn
them into functions, then the only interaction between the macro and the
namespace would be m_arg and m_ret.

The normal way you make macros would be gone, too. Macros are made from commands
that you've already executed, but $m_arg doesn't exist until you've made it into
a macro. You might be able to implement your way around it, but you would be
replacing one small special case (which isn't so special in the core1
refactoring) into a bunch of special cases.

Fernando and I had considered implementing macros by expanding them in the
"translation" phase (a la autocall), but we rejected it for some reason.

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

More information about the IPython-dev mailing list