-
-
Notifications
You must be signed in to change notification settings - Fork 381
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
Use field *repr* callable to dynamically define repr based on instance value #1193
Comments
Unfortunately, what you want it is currently not possible. The repr function only receives the value that's being repr-ed and the return value is added to the repr unconditionally: Lines 1874 to 1878 in c849440
|
@hynek Is this something you'd be interesting in having added. If so, I can look into the codebase and see if it falls into something I could make a PR for. |
If someone came up with a good PR, I would merge it. But I would strongly suggest to discuss your implementation approach with us first, before you put work into this that doesn't have a chance to be merged! |
@hynek that sounds reasonable to me. I've been looking at the code in attrs/src/attr/_make.py. I'm going to do a bit more investigation on where adding the repr fits into the order of things. I'll make a proposal if something seems workable. |
@hynek thanks for the work on
attrs
!Ideally, I'd only like a field to appear in a class' repr if that field is set to a non-default value. An example of this approach is scikit-learn's approach to printing class representations.
I'm trying to understand if this is already possible currently in
attrs
. I know thatattrs
accepts a callable for the repr parameter of anattrs.field
. But reading through the documentation, it is not clear what context (if any) is available to the repr. If the class' default values and the actual value of the parameter are as context to the callable, it would be relatively straightforward to craft a returned string that accomplished this.Can you provide some info on what's available to the callable so I can understand if this is possible?
The text was updated successfully, but these errors were encountered: