1

disclaimer first : I am writing a compiler so this is black voodoo stuff. I know it, I have put my protecting suit and I wouldn't do it If I could find any other simple way.

The source text is standard python and I transform it into something else using all the runtime type manipulation available. In short, all the standard types are enhanced with meta-data. Currently List is the only type which is not a leaf type and is standard. ( dict is in the same category but I didn't try yet to transform it, I would likely have the same problem ). Leaf types (int, str, bool, float) are ok, as are classes.

To elegantly replace the standard list by my enhanced list datatype is one swooop, I would like to change the default behavior of the token [] in python text, instead of returning a list, it should return my list-like type.

But I don't see any way to intercepting this token. ( I can override the token list via sys.modules[__builtins__], but [] seems beyond reach. )

Any idea ?

LBarret
  • 1,113
  • 10
  • 23
  • Are you talking about changing the method `list.__getitem__`? – jamylak Jun 07 '13 at 07:50
  • 2
    recompile the source code. – Elazar Jun 07 '13 at 07:51
  • 2
    @jamylak I understand he wants to override list literals. – Elazar Jun 07 '13 at 07:51
  • 2
    There is almost certainly a simpler, less hacky way. This reeks of [XY problem](http://meta.stackexchange.com/q/66377). If you want this for the code you write yourself, just explicitly add what you need. If it's for code your compiler generates, change your compiler to add explicitly what it needs. –  Jun 07 '13 at 07:51
  • Thank you for your questions/remarks. I updated the text to explain better the goals and constraint of the project. – LBarret Jun 07 '13 at 08:05

1 Answers1

2

As stated here, python doesn't allow you to override its syntax.

Although, this answer contains a hack that might help you (probably not).

Community
  • 1
  • 1
Martin Maillard
  • 2,751
  • 19
  • 24