<div dir="ltr"><br><br>On Sunday, May 14, 2017 at 3:05:46 AM UTC-4, Steven D'Aprano wrote:<blockquote class="gmail_quote" style="margin: 0;margin-left: 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></blockquote><div>Thanks!</div><div> </div><blockquote class="gmail_quote" style="margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">> 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></blockquote><div>    Right, the parent class would have to specifically written to allow that e.g. via:<br>    </div><div> def ParentClass(obj=None):</div><div>    self = obj or Object()</div><div>    ...</div><div> </div><blockquote class="gmail_quote" style="margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">Why is self an instance of the parent class, instead of MyClass?
<br></blockquote><div><br></div><div>That's what I've tried to cover under "(+/-) Checking types: ..."<br><br></div><blockquote class="gmail_quote" style="margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">
<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></blockquote><div>def MyClass would basically as a replacement for __init__. Using __init__ instead for your example would also not be perfect:<br><br></div><div>def __init__(self, x, y, z, a, b, c):</div><div>   self._x = x</div><div>   self._y = y</div><div>   ...<br><br></div><div>The point is though, that you could still do exactly the same if you wanted to:</div><div><br></div><div>def MyClass(x, y, z, a, b, c):</div><div>   self = SuperClass()</div><div>   self.x = x</div><div>   self._y = y</div><div>   ...</div><div>   </div><div>   def my_method():<br>     self.x += 5</div><div>     return self.x + self._y</div><div>   ...</div><div><br></div><blockquote class="gmail_quote" style="margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">
<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="javascript:" target="_blank" gdf-obfuscated-mailto="Y8IKeLUYAwAJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">Python...@python.org</a>
<br><a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank" rel="nofollow" onmousedown="this.href='https://www.google.com/url?q\x3dhttps%3A%2F%2Fmail.python.org%2Fmailman%2Flistinfo%2Fpython-ideas\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFj1EaNHnVmh20FnFPoUi4J-MpfQw';return true;" onclick="this.href='https://www.google.com/url?q\x3dhttps%3A%2F%2Fmail.python.org%2Fmailman%2Flistinfo%2Fpython-ideas\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFj1EaNHnVmh20FnFPoUi4J-MpfQw';return true;">https://mail.python.org/<wbr>mailman/listinfo/python-ideas</a>
<br>Code of Conduct: <a href="http://python.org/psf/codeofconduct/" target="_blank" rel="nofollow" onmousedown="this.href='http://www.google.com/url?q\x3dhttp%3A%2F%2Fpython.org%2Fpsf%2Fcodeofconduct%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHJOrArSUDKkjrnthO6_CznMzkPsA';return true;" onclick="this.href='http://www.google.com/url?q\x3dhttp%3A%2F%2Fpython.org%2Fpsf%2Fcodeofconduct%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHJOrArSUDKkjrnthO6_CznMzkPsA';return true;">http://python.org/psf/<wbr>codeofconduct/</a>
<br></blockquote></div>