[Python-ideas] JavaScript-Style Object Creation in Python (using a constructor function instead of a class to create objects)

Tin Tvrtković tinchester at gmail.com
Fri May 19 16:35:25 EDT 2017


Hello,

I'm an attrs contributor so maybe I can clear up any questions.

Convert callables are only called in __init__, not in the setters. We've
had this requested a number of times and we will almost certainly support
it in the future, probably on an opt-in basis.

The reason we don't currently support it is mostly technical. We try really
hard for our features to not add significant overhead to the generated
classes, and doing this with minimal overhead for slot classes basically
requires C/Cython or attribute *access* becomes significantly slower. But
this is implementation stuff and not pertinent here.

Date: Fri, 19 May 2017 20:49:08 +0200
> From: Stephan Houben <stephanh42 at gmail.com>
> To: guido at python.org
> Cc: "Eric V. Smith" <eric at trueblade.com>, Python-Ideas
>         <python-ideas at python.org>
> Subject: Re: [Python-ideas] JavaScript-Style Object Creation in Python
>         (using a constructor function instead of a class to create objects)
> Message-ID:
>         <CAOOa=pOBk8i1+sy+sZ4C+oLwhZzhLAjNRNiaEh8N9Srv=
> pxDNw at mail.gmail.com>
> Content-Type: text/plain; charset="utf-8"
>
> Hi Guido,
>
> Yes indeed, *only* invoked by __init__ .
>
> See my test below.
> =====
> import attr
>
> @attr.s
> class Foo:
>     x = attr.ib(convert=str)
>
> foo = Foo(42)
> print(repr(foo.x))
> # prints '42'
> foo.x = 42
> print(repr(foo.x))
> # prints 42
> ======
>
> Not sure if this is a good design but it matches the docs.
>
> Stephan
>
> Op 19 mei 2017 20:36 schreef "Guido van Rossum" <guido at python.org>:
>
> So it is only called by __init__ and not by __setattr__?
>
> On Fri, May 19, 2017 at 11:32 AM, Stephan Houben <stephanh42 at gmail.com>
> wrote:
>
> > Let me quote the attrs docs:
> >
> > ""
> > convert (callable) ? callable() that is called by attrs-generated
> __init__
> > methods to convert attribute?s value to the desired format. It is given
> the
> > passed-in value, and the returned value will be used as the new value of
> > the attribute. The value is converted before being passed to the
> validator,
> > if any.
> > """
> >
> > So the signature is essentially:
> >
> > self.myattrib = callable (myattrib)
> >
> > Stephan
> >
> > Op 19 mei 2017 20:25 schreef "Guido van Rossum" <guido at python.org>:
> >
> > For people who don't want to click on links:
> >>
> >> 1. Allow hash and equality to be based on object identity, rather than
> >> structural identity,
> >>    this is very important if one wants to store un-hashable objects in
> >> the instance.
> >>   (In my case: mostly dict's and numpy arrays).
> >>
> >> 2. Not subclassed from tuple. I have been bitten by this subclassing
> >> when trying to set up
> >>    singledispatch on sequences and also on my classes.
> >>
> >> 3. Easily allow to specify default values. With namedtuple this
> >> requires overriding __new__.
> >>
> >> 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 have one question about (4) -- how and when is the conversion
> function used, and what is its signature?
> >>
> >>
> >> On Fri, May 19, 2017 at 5:42 AM, Eric V. Smith <eric at trueblade.com>
> >> wrote:
> >>
> >>> Could you point me to this 4-point list of Stephan's? I couldn't find
> >>>> anything in the archive that you might be referring to.
> >>>>
> >>>
> >>> Never mind, I found them here:
> >>> https://mail.python.org/pipermail/python-ideas/2017-May/045679.html
> >>>
> >>> Eric.
> >>>
> >>>
> >>> _______________________________________________
> >>> Python-ideas mailing list
> >>> Python-ideas at python.org
> >>> https://mail.python.org/mailman/listinfo/python-ideas
> >>> Code of Conduct: http://python.org/psf/codeofconduct/
> >>>
> >>
> >>
> >>
> >> --
> >> --Guido van Rossum (python.org/~guido)
> >>
> >> _______________________________________________
> >> Python-ideas mailing list
> >> Python-ideas at python.org
> >> https://mail.python.org/mailman/listinfo/python-ideas
> >> Code of Conduct: http://python.org/psf/codeofconduct/
> >>
> >>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170519/a373c119/attachment.html>


More information about the Python-ideas mailing list