Python Macros

Michael Sparks zathras at thwackety.com
Mon Oct 4 15:43:51 EDT 2004


Arich Chanachai wrote:
>>>How hard/easy is it to extend the Python syntax?
> I was thinking Scheme.
...
> Really?  I read that Python was just like Scheme in that it had macro 
> facilities.

Ahh... I see where you're coming from. Unfortunately you've been misled on
this point - python does not have those sorts of macros. As a result if you
want to extend the language, ...

> :Local overhead???  Private branch???  How come?  I'm extending the
> language not adding another Python interpreter.

... the only way to do this is to modify the python interpreter itself,
which is why I gave the reference I did (since I've had experience lately
of doing this :-). ie you can't do this and not add another Python
interpreter.

The reason I suggested this is an overhead is because if you did that you
would end up with your own customised version of the python compiler-
something it sounds like you don't want. (not suprising) Furthermore there
aren't any tutorials that I'm aware of on doing this, which is why I
pointed you at one example of doing this :) (If only because it lets you
know what sort of what hoops you need to jump through to do it without
using a preprocessor, tokeniser, etc)

As a result your options are:
   * Implement your own pre-processor - either using the tokeniser module or
     using a full blown parser like PLY, SPARK, etc.
   * Implement the functionality you want using usual classes. You may or
     may not like that approach.
   * Find an existing macro pre-processor and modify it to do what you want.

Personally I'd choose option 2 here.

> Not sure I understand you here.  I want to implement messages like in
> ObjC where if you send a msg to an object and that object does know what
> to do with it, it can send the obj automatically to another object which
> might know what to do with it.

I've not used ObjC - a code fragment with a brief explanation would
probably help. (That description matches too many possible use cases
and solutions... :) Posting a fragment *might* also jog someone's memory
to be able to say "that's already implemented -- here" :-)

> Anyhow, there are other reasons as well.

Without knowing these it's difficult to help more. (And since you're asking
for tutorials it's not clear at all where to point you to without seeing
something concrete.

Regards,


Michael.




More information about the Python-list mailing list