[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 12:38:18 EDT 2017


Hi Sven,

> But even given that (and I am only speaking for my team), I haven't even
> seen a use-case for namedtuples in a year. Every time we considered it,
> people said: "please make it its own class for documentary purposes; this
> thing will tend to grow faster than we can imagine".

Using namedtuple doesn't stop the class from being its "own class".
Typical use case:

 class Foo(namedtuple("Foo", "bar "baz"), FooBase):
      "Foo is a very important class and you should totally use it."""

       def grand_total(self):
            return self.bar + self.baz


Stephan


2017-05-17 18:11 GMT+02:00 Sven R. Kunze <srkunze at mail.de>:
> On 17.05.2017 13:37, Michel Desmoulin wrote:
>>
>> Having a cleaner, faster solution to declare a class would be awesome,
>> both for dev and for teaching. That's why we all love attrs.
>>
>> But we are talking here about a nice-to-have feature. Python works
>> perfectly fine without it. But since we are at it, let's make something
>> great.
>>
>
> Same for me. IMHO the biggest benefit using attr is an (almost?)
> feature-complete and bug-free set of pre-defined __dunder__ methods as
> described in [1].
>
> Defining state-variables (aka instance variables accessible via 'self.')
> wouldn't be enough for me to make it a valuable feature. So, one could
> imagine a __dunder__-method generator of some sort.
>
>
> But even given that (and I am only speaking for my team), I haven't even
> seen a use-case for namedtuples in a year. Every time we considered it,
> people said: "please make it its own class for documentary purposes; this
> thing will tend to grow faster than we can imagine".
>
>>> 2. Is it really that complicated? attr.s is just a normal Python
>>> function, which adds some members to a class.
>>>    You don't even have to use the decorator @ syntax, that is just a
>>> convenience.
>>>   To me this seems easier to teach than yet another dedicated syntax.
>>
>> My guess is that, without the decorator, the attributes don't do
>> anything. They are just declarative hints for the decorator to do the
>> magic.
>>
>> But even then, it's already an additional burden to have to explain the
>> difference between this magic and the regular class attribute.
>
>
> It might also have something to do with this. IMO this feature should
> integrate naturally in a way that nobody notice.
>
>
>
> Sven
>
> [1] https://attrs.readthedocs.io/en/stable/why.html#hand-written-classes
>
>
>
> _______________________________________________
> 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/


More information about the Python-ideas mailing list