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

Stephan Houben stephanh42 at gmail.com
Wed May 17 02:49:46 EDT 2017


Hi Guido,

As mentioned I am a heavy user of namedtuple, I use it everywhere
where constructor arguments are equal to instance variables.
Which is quite often, at least in my programming style
(possibly under the influence of ML's "datatype" and Scala's "case class"-es.)

Compared to namedtuple, I see that attr solves a number of issues
which sometimes
prevented me from using namedtuple:

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)


These are the main advantages I have encountered so far.

Stephan


2017-05-16 23:04 GMT+02:00 Guido van Rossum <gvanrossum at gmail.com>:
> Stephen,
>
> What features of attrs specifically solve your use cases?
>
> --Guido
>
> On Tue, May 16, 2017 at 12:18 PM, Stephan Houben <stephanh42 at gmail.com>
> wrote:
>>
>> Hi all,
>>
>> Thanks to this thread I learned about the "attrs" library. I am a
>> heavy namedtuple (ab)user but I think
>> I will be using attrs going forward.
>>
>> If something like attrs would made it in the standard library it would
>> be awesome.
>>
>> Thanks,
>>
>> Stephan
>>
>> 2017-05-16 20:08 GMT+02:00 Brett Cannon <brett at python.org>:
>> > Maybe we can bring this up as a lightning talk at the language summit to
>> > see
>> > who in the room has the appropriate background knowledge? And obviously
>> > someone can talk to Hynek to see if he wants to provide input based on
>> > community feedback for attrs and lessons learned.
>> >
>> > On Tue, 16 May 2017 at 08:11 Guido van Rossum <gvanrossum at gmail.com>
>> > wrote:
>> >>
>> >> Maybe Lukasz is interested?
>> >>
>> >> On May 16, 2017 8:00 AM, "Chris Angelico" <rosuav at gmail.com> wrote:
>> >>>
>> >>> On Wed, May 17, 2017 at 12:53 AM, Guido van Rossum <guido at python.org>
>> >>> wrote:
>> >>> > I could also try this myself in my spare time at PyCon
>> >>> > (surprisingly, I
>> >>> > have
>> >>> > some!). It sounds kind of interesting. However I've never used the
>> >>> > 'attrs'
>> >>> > package...
>> >>>
>> >>> Me neither, so I'm not really an ideal person to head this up. Is
>> >>> there anyone who (a) knows what is and isn't Pythonic, (b) has used
>> >>> 'attrs', and (c) has spare time? It's not an easy trifecta but we can
>> >>> hope!
>> >>>
>> >>> ChrisA
>> >>> _______________________________________________
>> >>> 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/
>> >>
>> >> _______________________________________________
>> >> 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/
>> >
>> >
>> > _______________________________________________
>> > 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)


More information about the Python-ideas mailing list