Add takes_self to Factory and @_CountingAttr.default #189
Now this is a biggie but I believe it’s worth it for 17.1. There should be no performance penalty but it’s a feature that people have been yearning for since day one. :)
Let me know what you think, contains some boy scouting. Most notably the completely hosted markup in api.rst around evolve. Also WTF was with local dunder variables in methods? I suppose copy pasta?
This enables the following:
@attr.s class C: x = attr.ib(default=1) y = attr.ib(default=attr.Factory(lambda self: self.x + 1, takes_self=True)) z = attr.ib() @z.default def whatever(self): return self.y + 1
@@ Coverage Diff @@ ## master #189 +/- ## ===================================== Coverage 100% 100% ===================================== Files 9 9 Lines 560 579 +19 Branches 124 126 +2 ===================================== + Hits 560 579 +19
This is somewhat evil, but have you considered counting the number of arguments to the factory function, instead of taking
OK, I'm starting looking at this. Let's talk API first :)
Another suggestion. I've been mulling over #178 with spare brain cycles. I've come to realize the wordy
could be rewritten like this:
To me this reads much better and is possible today. Honestly I think I would prefer this rather than allow both
What if we actually implemented
You can't really decorate a value so I'm OK with the fact the decorator is applied to a method and the
I'll take a look at the implementation first thing I have the chance. You said you wanted this before PyCon, that's on Wednesday?
Given the following test class:
this init gets generated:
which is fine, nothing wrong with it. However, we can speed it up, as always, by shifting work. This init does a dict lookup (on attr_dict), then an instance look up on that, then a method call. This is unnecessary. If we just inject the factory into the globals and generate an init like this:
my benchmark goes from 1.69 us +- 0.05 us to 1.38 us +- 0.07 us, which is significant (~22% speedup).
There, now the ball is in your court :p