On 5/31/2018 10:49 AM, Dan Strohl via Python-list wrote:
Is it possible to override the assignment of built in types to the shorthand 
representations?

By which I presume you mean literals and overt (non-comprehension) displays. So you wish that Python should be even more dynamic. (Some wish it were less so ;-)

The CPython parser is generated by a parser-generator program from the python grammar and a table of non-default functions to call when the parser recognizes certain grammatical productions. For instance, *stringliteral* is mapped to str. I assume that the mapping is currently direct, and not routed through the builtins dict. I don't know what other implementations do.

To change this, I believe you would have to introduce indirect mapping functions. One possibility would be C equivalents of functions like

def get_str(): return builtins['str']

Then you could change future parsing by executing
  builtins['str'] = mystr\

However, this would affect the parsing of imported modules, if and when they are parsed, and exec and eval calls made within imported functions. This would not be good.

So I believe you would also need to introduce a module copy of a subset of builtins, call it '__modclass__', whose keys would be the classes called by the parser, and use that in the get_xyz functions. Then I believe you could change parsing within a module by executing
   __modclass__['str'] = mystr

and yes, I know I could simply not do [] and always do my_list('item1', 
'item2', 'item3')

I think we should stick with this.

This would only be scoped to the current module and would not be imported when 
that module was imported.

The harder part, I think, is "and not affect parsing of imported modules if they are not already parsed and not affect exec and eval calls in imported modules.


--
Terry Jan Reedy

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to