Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Parser element custom name isn't appropriate for __repr__ #416

Open
@djpohly

Description

@djpohly

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions