Skip to content

Use field *repr* callable to dynamically define repr based on instance value #1193

Open
@RNKuhns

Description

@RNKuhns

@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 that attrs accepts a callable for the repr parameter of an attrs.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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions