[Python-ideas] JavaScript-Style Object Creation in Python (using a constructor function instead of a class to create objects)
Juancarlo Añez
apalala at gmail.com
Tue May 16 23:14:14 EDT 2017
On Tue, May 16, 2017 at 5:04 PM, Guido van Rossum <gvanrossum at gmail.com>
wrote:
What features of attrs specifically solve your use cases?
>
(not Stephen)
I hadn’t thought about this use case:
In [1]: class C():
...: x = 1
...:
...: def __init__(self, x=None):
...: if x is not None:
...: self.x = x
...:
...: def __str__(self):
...: return 'C(%s)' % self.x
...:
In [2]: c1 = C()
...: c2 = C(2)
...:
In [3]: print(c1, c2)
C(1) C(2)
And I might use it here on.
What I like about attrs is:
- The class level declaration of instance attributes
- That the reasonable *init*, *repr*, and *eq* are generated
I don’t like the excessive wordiness in attrs, 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.
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.
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.
--
Juancarlo *Añez*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170516/798b4257/attachment.html>
More information about the Python-ideas
mailing list