Skip to content

More convenient shorthand for derived attributes #310

@gabbard

Description

@gabbard

Resolving #165 allowed setting the default for an attribute based on other attributes using a decorator like this:

@attrs(frozen=True, slots=True)
class Foo:
   bar = attr.ib()
   moo = attr.ib()

   @moo.default
   def _init_moo(self):
      return self.bar + 2

Many times in frozen classes we want to derive one attribute from another without giving the user the opportunity to override it. The Immutables library for Java provides for this via Value.Derived: http://immutables.github.io/immutable.html#derived-attributes . Perhaps attrs could have a @field.derived decorator which does the same thing?

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