Skip to content

attrs misconceptions #4

@hynek

Description

@hynek

Hi,

I would like to correct some attrs misconceptions. :)

  1. Custom __init__: you can always pass @attrs(init=False) and no __init__ is generated so that doesn’t seem like a fair criticism (basically: you have to pass an argument :))?
  2. Subclassing: attrs has wonderful support for sub-classing; it just works a bit differently. From the examples page:
>>> @attr.s
... class A(object):
...     a = attr.ib()
...     def get_a(self):
...         return self.a
>>> @attr.s
... class B(object):
...     b = attr.ib()
>>> @attr.s
... class C(B, A):
...     c = attr.ib()
>>> i = C(1, 2, 3)
>>> i
C(a=1, b=2, c=3)
>>> i == C(1, 2, 3)
True
>>> i.get_a()
1

In other words: if you’re composing attrs classes, everything works out of the box as you’d wish to. If you’re mixing in 3rd party classes it’s more complicated and you’ll have to write your own __init__.

Cheers,
—h

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions