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

Michel Desmoulin desmoulinmichel at gmail.com
Wed May 17 07:37:22 EDT 2017

Le 17/05/2017 à 13:08, Stephan Houben a écrit :
> Hi Michel,
>> Now OPP is already hard to teach to my students, but if I have to add
>> this to the mix, I will just have to tell them to copy / paste it
>> blindly for a long time before I can get to the point they can
>> understand what it does.
> About the teachability, some remarks:
> 1. You can still just skip it. I presume you don't teach all the
> advanced metaclass
>    stuff right away either, even though it is part of core Python.

Comparing setting instance attributes to metaclasses is pushing it don't
you think ?

The thing is, teaching OOP is challenging enough. You have __init__ and
all it's quicks, and the @decorators for class methods, and self, and
cls, and inheritances. And that's just the basics, not talking about
multiple inheritance, properties, other dunder methods or compostion.

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

> 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. So if I
have to do it, I wish I could do it without having to explain also 2
namespaced function calls, one taking itself the return value of a call
as a parameter, which BTW is a factory taking a constructor as a callback.

Do not over estimate the skills of newcommers in programming. They have
a lot of things to learn.

More information about the Python-ideas mailing list