-
-
Notifications
You must be signed in to change notification settings - Fork 383
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
Feature Request: Custom __repr__ for particular attribute? #567
Comments
wow. i've been using thanks for writing it up @peteroconnor-bc, this is exactly what i would like to have as well. |
here's a preliminary pull request to explore what it could look like: #568 |
I have mixed feelings about this, but maybe we can sort this out. My plan is to make the whole creation of methods pluggable (I have to admit that I have been lazy or swamped – depending on how charitable you are). I.e. you pass a method-factory to However that doesn't seem to preclude the need to modify the behavior of single attributes so I guess who could do both? |
for class level custom repr, one can already pass this means the main (only?) usefulness of custom repr callables is to have this on a per-attribute level, e.g. you don't want your and as my preliminary pull request shows, this is mostly an extension of existing behaviour without any backward compatibility concerns. the |
@wbolster nice, was not expecting someone to implement this already |
I also was just reviewing some code using |
Yes, but then you need to know a bunch of E.g., take the use case in #41 itself, where the logic at the class level now needs to be "Ask all the attrs for their own reprs, then give me those, and I know how to assemble a class-whole one". Still think there's a chance it might be nice to have some class-level help, but yeah possibly that can decouple from this since obviously the attr-specific stuff is even more important, can always come back for the other side if it continues to be annoying (I suspect for me it likely will be personally, since I essentially always want #41 as my repr, but yeah...). |
yeah let's keep class repr separate. incidentally, it was me who opened #41 ;) |
closing this issue because it is pretty much identical to #212. |
* wip: support custom repr() callable for attributes see #567 * extend ‘repr=...’ arg type in .pyi stubs * expand docstring for attr.ib() * add changelog entry * add docs with example * improve my copy/paste skills 🙈 * fix grammar * fix typo in changelog entry * fix and improve attrib() docstring * detect custom repr() once, not per call. be strict about bool. * use rst syntax, not markdown * apply hynek's suggestions for changelog entry * add ‘versionchanged’ note in docstring * add custom attribute repr= to typing example * simplify comment
Reading the docs, it seems that there's no ability to do a custom repr for a particular attribute. E.g., It would be nice to be able to specify a function for
repr
like:so that when you
print(Datapoint(img, lab))
, it does not display numpy's awkward default formatting.As far as I see you can currently only set a boolean flag
repr=False
to disable to whole attribute from being shown in the object's repr, which is not what I want. We could still maintain this behaviour if we just sayrepr: Optional[Union[bool, Callable[[Any], str]]]=True
The text was updated successfully, but these errors were encountered: