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

Guido van Rossum gvanrossum at gmail.com
Wed May 17 01:22:00 EDT 2017

On Tue, May 16, 2017 at 8:14 PM, Juancarlo Añez <apalala at gmail.com> wrote:

> What I like about attrs is:
>    - The class level declaration of instance attributes
>    - That the reasonable *init*, *repr*, and *eq* are generated
> OK, the former should be doable using PEP 526 (the type is stored in
__annotations__ and the default in the class dict given to the metaclass),
while the latter should be doable using a standard metaclass -- assuming we
can agree on what the "reasonable" __init__, __repr__ and __eq__ should do.

> I don’t like the excessive wordiness in attrs,
Really? @attr.s is wordy? :-) I think it's deadly cute. (The only library
I've ever seen that did something worse was "monocle" which used @_o.)

> and I don’t need “the kitchen sink” be available to have instance
> attributes declared at the class level. A solution based on the typing
> module would be much better.
That's what I am hoping, yes.

> Basically, Python is lacking a way to declare instance fields with default
> values away of the initializer. Several of the mainstream OO languages
> (Java, Swift, Go) provide for that.
Hm, there are some issues here of course -- while it's simple to set the
default to e.g. 0, (1, 2, 3) or '<string>', it's not so easy to set a
default to [] or {'foo': 'bar'} unless you just state "do whatever
copy.copy() does".

> I haven’t thought much about this, except about if there’s indeed a need
> (and there is), but I can’t know if the solution if through decorators, or
> inheritance.
I suppose we could do it using either a class decorator or a metaclass --
we'll have to compare the pros and cons and specific use cases to choose.
(Example: https://github.com/python/typing/issues/427.)

--Guido van Rossum (python.org/~guido <http://python.org/%7Eguido>)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170516/7afcbda4/attachment-0001.html>

More information about the Python-ideas mailing list