Code Monkey home page Code Monkey logo

Comments (9)

hynek avatar hynek commented on May 9, 2024

So the problem here is that when z is defined, foo doesn’t exist yet.

It would be possible to pass self into factories but they’d have to be functions which kind of defeats the purpose, right?

I wonder if something like:

@attr.s(post=True)
class C:
    x = attr.ib(init=False)

   def __post_init__(self):
      """
      Called at the end of __init__.
      """
      self.x = "whatever"

would make sense?

from attrs.

econchick avatar econchick commented on May 9, 2024

I think so - if class C inherited from another class, e.g. class B, can __post__ access attributes defined in class B?

from attrs.

hynek avatar hynek commented on May 9, 2024

I’m thinking that attrs will generate:

def __init__(self):
    self._x = None
    self.__post_init__()

for you.

from attrs.

grahamegee avatar grahamegee commented on May 9, 2024

Personally I would like to be able to mix and match having the attr generated __init__ along with some user defined __init__, and be able to decide which is executed first. Is there currently any way at all to execute user defined initialisation other than not decorating your class with @attr.s?

from attrs.

wearpants avatar wearpants commented on May 9, 2024

@hynek I could use something similar to your __post_init__() as well, but I'd like the usual attr-provided __init__ to be called first. My use case is an object that takes a regex string as an arg and saves a compiled versions to a private attribute.

Currently I'm doing this with a classmethod constructor:

@attr.s
class A:
     some_regex = attr.ib()

    @classmethod
    def create(cls, *args, **kwargs):
        self = cls(*args, **kwargs)
        self._some_regex = re.compile(self.some_regex)
        return self

but this is less than ideal / breaks user expectations since the way to make an A instance is A.create(..) instead of A(..) (which gives you a half-built object).

I initially implemented a version using __init__ that (ab)used side effects in a validator to do the compilation, but then decided that was too hacky even for me.

from attrs.

hynek avatar hynek commented on May 9, 2024

Why would you say "but"? That's the whole point of post. :)

from attrs.

wearpants avatar wearpants commented on May 9, 2024

I was kinda confused by signature of __init__ here and here not taking any arguments, and the value being set in post_init, but I think that's specific to x = attr.ib(init=False)

If we had:

@attr.s
class A:
    x = attr.ib()

    def __post_init__(self):
        assert hasattr(self, 'x')

I'd expect attrs to generate something like:

class A:
    def __init__(self, x):
        self.x = x
        self.__post_init__()

Sorry if I'm confusing things, been up since 4 AM for no good reason.

from attrs.

hynek avatar hynek commented on May 9, 2024

heh yeah my current plan is what you'd expect. This but thread started in a different direction until it settled on this. :)

I think I'll tackle it once immutably is in.

from attrs.

hynek avatar hynek commented on May 9, 2024

Closing in favor of #68 which contains a more succinct description of what we came up with.

from attrs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.