Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

How should I get an attribute default (including calling a factory)? #545

Copy link
Copy link
Closed
@altendky

Description

@altendky
Issue body actions

I was helping out in #python and there was a question about setting an instance attribute back to default. I didn't find a way provided by attrs to do that . Did I miss it? If not, would that be of interest? I haven't thought much about how it should be provided but here's the implementation I wrote for them.

https://repl.it/@altendky/UnpleasantUnconsciousPi-1

def get_default(instance, attribute):
    default = attr.fields_dict(type(instance))[attribute].default
    if isinstance(default, attr.Factory):
        if default.takes_self:
            return default.factory(instance)

        return default.factory()
    
    return default

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

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.