Open
Description
The Python documentation on __repr__
encourages developers to return something that "look[s] like a valid Python expression that could be used to recreate an object with the same value" or that is at least "information-rich and unambiguous".
The custom name from set_name
isn't really appropriate for this. Always using the shorthand "default name" for __repr__
would be more information-rich, although something that works like this would really be ideal:
>>> p = ZeroOrMore(one_of(["hello", "world"]) + "foo", stop_on=Keyword('end'))
>>> print(p) # calls str()
[{hello | world 'foo'}]...
>>> p # calls repr()
(Regex('hello|world') + 'foo')[...: Keyword('end')]
>>> q = eval(repr(p)) # nice!
>>> q.set_name("custom") # name doesn't affect repr()
(Regex('hello|world') + 'foo')[...: Keyword('end')]
>>> print(q) # name does affect str()
custom
All of the above I have working so far for a handful of ParserElement types. Interested in a complete PR?
(And since I seem to tie everything into documentation: Sphinx uses repr()
to show the values of documented variables.)
Metadata
Metadata
Assignees
Labels
No labels