Skip to content

Post init hook called after class is frozen. #120

@jkbbwr

Description

@jkbbwr

Post init hook is called after frozen, not allowing adding variables that only make sense in an instance context.

import attr
import logbook
import sys
from logbook import StreamHandler

StreamHandler(sys.stdout).push_application()


@attr.s
class Example(object):
    x = attr.ib()
    y = attr.ib()

    def __attrs_post_init__(self):
        # bypass frozen on lower classes
        # self._log = logbook.Logger(self.__class__.__name__) causes a Frozen exception in child
        object.__setattr__(self, "_log", logbook.Logger(self.__class__.__name__))


@attr.s(frozen=True)
class Child(Example):
    def do(self):
        self._log.info("hi")


Child(1, 2).do()

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