<div dir="auto">Whatever you all propose,<div dir="auto"><br></div><div dir="auto">coming from a java and c++ background, OOP in python is quite cumbersome.</div><div dir="auto"><br></div><div dir="auto">if you tell that i am not a python guy, then consider that current oop style does not reflect python's style of ease and simplicity</div><div dir="auto"><br></div><div dir="auto">is __init__ really a good syntax choice?</div><div dir="auto"><br><div data-smartmail="gmail_signature" dir="auto">Abdur-Rahmaan Janhangeer,<br>Mauritius<br><a href="https://abdurrahmaanjanhangeer.wordpress.com">https://abdurrahmaanjanhangeer.wordpress.com</a></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On 14 May 2017 11:05, "Steven D'Aprano" <<a href="mailto:steve@pearwood.info">steve@pearwood.info</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On Sun, May 14, 2017 at 04:07:44AM +0000, Simon Ramstedt wrote:<br>
> Hi, do you have an opinion on the following?<br>
<br>
Hi, and welcome, and of course we have an opinion! This is Python-Ideas,<br>
we're very opinionated :-)<br>
<br>
> Wouldn't it be nice to define classes via a simple constructor function (as<br>
> below) instead of a conventional class definition?<br>
<br>
No.<br>
<br>
> *conventional*:<br>
><br>
>     class MyClass(ParentClass):<br>
>       def __init__(x):<br>
>         self._x = x<br>
>       def my_method(y):<br>
>         z = self._x + y<br>
>         return z<br>
<br>
Looks good to me. It is nicely explicit that you're creating a class,<br>
the superclass or superclasses are easy to see, and the attributes are<br>
explicit.<br>
<br>
<br>
> *proposed*:<br>
><br>
>     def MyClass(x):<br>
<br>
That is the exact same syntax for defining a function called "MyClass",<br>
that takes one argument, x. How is Python (and the reader!) supposed to<br>
tell which calls to def return a class and which return a function?<br>
<br>
<br>
>       self = ParentClass()<br>
<br>
What if you have multiple parent classes?<br>
<br>
Why is self an instance of the parent class, instead of MyClass?<br>
<br>
<br>
>       def my_method(y):<br>
>         z = x + y<br>
>         return z<br>
<br>
The local variable x is not defined. Wait, is that supposed to come from<br>
the closure def MyClass(x)?<br>
<br>
What if your class has twenty methods, each of which takes different<br>
arguments? Do you have to write:<br>
<br>
def MyClass(x, # used in my_method<br>
            y, # used in another_method<br>
            z, # used in third_method,<br>
            a, b, c, # used in fourth_method<br>
            ...  # blah blah blah<br>
            ):<br>
<br>
How does this generalise to non-toy classes, classes with more than one<br>
method?<br>
<br>
<br>
>       self.my_method = my_method  # that's cumbersome (see comments below)<br>
>       return self<br>
><br>
><br>
> Here are the pros and cons I could come up with for the proposed method:<br>
><br>
> (+) Simpler and more explicit.<br>
<br>
I think you mean "More complicated and less explicit".<br>
<br>
Is this supposed to be some sort of prototype-based OOP instead of<br>
class-based OOP? I'd be interested in investigating prototype-based<br>
objects, but I don't think this is the way to do it.<br>
<br>
<br>
<br>
--<br>
Steve<br>
______________________________<wbr>_________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/<wbr>mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/<wbr>codeofconduct/</a><br>
</blockquote></div></div>