Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Almost all Python classes implement __repr__ incorrectly, preventing normal use of help or printing of nested structures. #8542

Closed
exalate-issue-sync bot opened this issue May 12, 2023 · 1 comment

Comments

@exalate-issue-sync
Copy link

Python {{repr}} function should not print anything to the console, it should just return an internal representation of the instance, that can ideally be processed.
[https://docs.python.org/3/reference/datamodel.html#object.repr|https://docs.python.org/3/reference/datamodel.html#object.repr]

In the same way, {{str}} should not call {{print}} either, just return the string that's is going to be printed when user calls {{print}} on the object!
[https://docs.python.org/3/reference/datamodel.html#object.str|https://docs.python.org/3/reference/datamodel.html#object.str]

Many objects in h2o, also implement {{show}}: this methods is used to print complex objects.
Although, it seems to serve the same goal as {{print(obj)}}.
We can keep it for backwards compatibility but the printable string of an instance should be the result of {{str}} implementation, so that:

{code:python}def show(self):
print(self){code}

Among the side effects of this:

  • in IPython/Jupyter instance details are printed when inspecting method using {{inst.method?}}, {{inst.method??}}.
  • for debugging, for example when printing request results, we don't have the choice between printing {{repr()}} or {{str()}} (default), so that it prints some of its nested values BEFORE its keys... making this unreadable, non-parsable...

As a default implementation for {{repr}} in H2O classes, I would suggest something like:

{noformat}def repr(self):
r = dict(class='.'.join([self.class.module, self.class.name])
r.update(self.dict)
return repr(r){noformat}

@h2o-ops
Copy link
Collaborator

h2o-ops commented May 14, 2023

JIRA Issue Migration Info

Jira Issue: PUBDEV-7098
Assignee: Sebastien Poirier
Reporter: Sebastien Poirier
State: Resolved
Fix Version: 3.38.0.1
Attachments: N/A
Development PRs: Available

Linked PRs from JIRA

#6199
https://github.com/h2oai/h2oai-serving/pull/1079

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant