[Python-ideas] JavaScript-Style Object Creation in Python (using a constructor function instead of a class to create objects)
Steven D'Aprano
steve at pearwood.info
Fri May 19 20:19:23 EDT 2017
On Fri, May 19, 2017 at 11:24:53AM -0700, Guido van Rossum wrote:
> 4. Easily allow to specify a conversion function. For example I have
> some code like below:
> note that I can store a numpy array while keeping hashability and
> I can make it convert
> to a numpy array in the constructor.
>
> @attr.s(cmp=False, hash=False)
> class SvgTransform(SvgPicture):
> child = attr.ib()
> matrix = attr.ib(convert=numpy.asarray)
I find that completely enigmatic, there's far too much implicit
behaviour going on behind the scenes. I couldn't even begin to guess
what SvgTransform as a class does, or what SvgTransform.child and
SvgTransform.matrix are.
I suppose that's okay for experts to whom the attrs module is second
nature, but I think this approach is far too "magical" for my tastes.
Instead of trying to cover every possible use-case from a single
decorator with a multitude of keyword arguments, I think covering the
simple cases is enough. Explicitly overriding methods is not a bad
thing! It is much more comprehensible to see an explicit class with
methods than a decorator with multiple keyword arguments and callbacks.
I like the namedtuple approach: I think it hits the sweet spot between
"having to do everything by hand" and "everything is magical".
--
Steve
More information about the Python-ideas
mailing list