[Python-ideas] non-Pre-PEP: Syntactic replacement of built-in types with user-defined callables (Take 2... fewer newlines!)

Stephen J. Turnbull stephen at xemacs.org
Thu Jan 31 22:18:28 CET 2008

Taro writes:

 > > Since the way this would work is that each TYPE would have a
 > > string-to-TYPE-value converter, you would just do (inside the
 > > compiler)
 > >
 > > TYPE.stringconverter = STRINGCONVERTER
 > >
 > > and if the compiler encountered a literal with an undefined
 > > .stringconverter, it would generate an error to the effect of
 > >
 > >     Use of TYPE stringconverter 'STRINGCONVERTER' before definition.
 > [...provided that TYPE.stringconverter is set]
 > If I understand your intent correctly, as things stand this would
 > ordinarily be a runtime NameError,

That is exactly my intent.  A more explicit error message than
"NameError: name 'myTypeLexer' not defined" would be nice,
although if you give the function a reasonably explicit name it should
be clear enough.

However, a real problem with this idea is that I don't know if the
compiler knows how to call the functions it has just compiled!

Also, as syntax in general this kind of order dependence would be
considered unPythonic, I suspect.

 > and extra code would need to be
 > added to the compiler to keep track of that:
 >    from decimal import Decimal with replacingsyntax float as float
 >    x = 1.01                  # SyntaxError here???
 >    def Decimal():....

No, it would be a NameError, the name of the implicitly called
converter is not defined in scope.  The syntax is correct.

 > For this, the "solution" to keep precision would be along the lines of:

 >     ## mycollections.py
 >     class MyListFloatHelper(float):
 >         def __init__(self, value):
 >             self._strvalue = value
 >     class MyList(list):
 >         def __init__(self, inlist):
 >             for elem in inlist:
 >                 try:
 >                     elem = Decimal(elem._strvalue)
 >                 except AttributeError:
 >                     pass
 >                 self.append(elem)
 >     ## main.py
 >     from mycollections import MyListFloatHelper with readsyntax float
 >     from mycollections import MyList with readsyntax list
 >     def foo():
 >         myfloat = 1.000000000000000000000000000001
 >         mydlist = [1.0000000000000000000000000001]
 >         mydecimal = mydlist[0]
 >         print myfloat,"/", mydecimal
 >         #==> 1.0 / Decimal("1.0000000000000000000000000001")
 >         print type(myfloat), "/", type(mydecimal)
 >         #==> <class 'mycollections.MyListFloatHelper'> / <class 'decimal.Decimal'>

You've missed the point.  I want myfloat to be a Python builtin float
for some reason.  Only in the context of a dlist do I want that
read syntax to be parsed as a Decimal.

You can just require that literal read syntax replacements be global,
I guess, but you need to say something about whether this is going to
be possible in the proposal.

More information about the Python-ideas mailing list