[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 09:20:16 EDT 2017
Hi Michel,
> Comparing setting instance attributes to metaclasses is pushing it don't
> you think ?
My only point is that there are aspects of the core Python language which are
probably not covered by your course (and I chose metaclasses as a topic least
likely to be covered in a beginner's course).
So this would justify skipping an 'attr' functionality if it would be
deemed too complex
for beginners.
> 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.
I mean you don't need to use the @ syntax.
You can just as well do:
class MyClass:
foo = attr.ib()
MyClass = attr.s(MyClass)
thereby stressing that `attr.s' is just a plain Python function,
and there is no magic involved.
> Do not over estimate the skills of newcomers in programming. They have
> a lot of things to learn.
I realize that. My point is only about the *relative* ease of learning
of the current attr.s
decorator-based approach vs. dedicated syntax. Perhaps both are too
difficult for a beginner's
course, but I would contend that the decorator approach is (relatively) simpler
(perhaps in the same way that special relativity is simple compared to
general relativity ;-) ).
My argument for that is that the attr.s approach does not introduce
any new mechanism,
but is just an (advanced) application of basic mechanism such as
function calls and attribute access.
Hope this clarifies my position.
Stephan
2017-05-17 13:37 GMT+02:00 Michel Desmoulin <desmoulinmichel at gmail.com>:
>
>
> 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
> great.
>
>
>> 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.
>
>
> _______________________________________________
> 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