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

Brett Cannon brett at python.org
Mon May 15 12:50:04 EDT 2017


On Mon, 15 May 2017 at 08:30 Guido van Rossum <guido at python.org> wrote:

> This should be worked into a PEP, instead of living on as a bunch of
> python-ideas posts and blogs.
>
> I find the attrs documentation (and Glyph's blog post about it) almost
> unreadable because of the exalted language -- half the doc seems to be
> *selling* the library more than *explaining* it. If this style were to
> become common I would find it a disturbing trend.
>
> But having something alongside NamedTuple that helps you declare classes
> with mutable attributes using the new PEP 526 syntax (and maybe a few
> variants) would definitely be useful. Will someone please write a PEP? Very
> few of the specifics of attrs need be retained (its punny naming choices
> are too much for the stdlib).
>

In case someone decides to take this on, I wrote a blog post back in March
that shows how to use __init_subclass__() to do a rough approximation of
what Guido is suggesting:
https://snarky.ca/customizing-class-creation-in-python/ .

Based on my thinking on the topic while writing my blog post, the tricky
bit is going to be deciding how to handle default values (i.e. if you set a
default value like `attr: int = 42` on the class definition then you have
`cls.attr` exist which might not be what you want if you would rather have
the default value explicitly set on every instance but not fall through to
the class (e.g. `del ins.attr; ins.attr` raises an AttributeError instead
of falling through to `cls.attr`). You could remove the default from the
class in your __init_subclass__(), but then you have to decide if that's
too unexpected/magical for someone looking at the code.

And I too would be interested in seeing something like this, if for any
other reason than to help people not to misuse NamedTuple for
quick-and-dirty data objects in new APIs (NamedTuple is meant to help move
old-style tuple-based APIs to a class-based one).

-Brett


>
> --Guido
>
> On Mon, May 15, 2017 at 4:05 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:
>
>> On 14 May 2017 at 17:12, Abdur-Rahmaan Janhangeer <arj.python at gmail.com>
>> wrote:
>> > Whatever you all propose,
>> >
>> > coming from a java and c++ background, OOP in python is quite
>> cumbersome.
>> >
>> > if you tell that i am not a python guy, then consider that current oop
>> style
>> > does not reflect python's style of ease and simplicity
>> >
>> > is __init__ really a good syntax choice?
>>
>> That's a different question, and one with a well-structured third
>> party solution: https://attrs.readthedocs.io/en/stable/
>>
>> See https://mail.python.org/pipermail/python-ideas/2017-April/045514.html
>> for some ideas on how something like attrs might be adapted to provide
>> better standard library tooling for more concise and readable class
>> definitions.
>>
>> Cheers,
>> Nick.
>>
>> --
>> Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
>> _______________________________________________
>> 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/20170515/c7236622/attachment-0001.html>


More information about the Python-ideas mailing list