[Python-ideas] An even simpler customization of class creation

Andrew Barnert abarnert at yahoo.com
Mon Mar 9 13:40:03 CET 2015


On Mar 8, 2015, at 5:22 PM, Nick Coghlan <ncoghlan at gmail.com> wrote:
> 
> On 9 Mar 2015 10:12, "Nick Coghlan" <ncoghlan at gmail.com> wrote:
> >
> >
> > On 9 Mar 2015 03:18, "Mark Young" <marky1991 at gmail.com> wrote:
> > >
> > > Thanks Nick! I swear I checked on the 3.x "Data Model" page, but I guess not.
> >
> > Every other reference on that page is to the class attribute rather than the implicit method nonlocal, so it took a moment for me to relocate it myself. I should have searched for "super" instead :)
> 
> With the benefit of hindsight, if I had the chance to do this over, I'd suggest we call the implicit method nonlocal "__classdef__" instead, specifically to avoid the name collision with the runtime type information on object instances. My general impression is that the current name triggers too many incorrect assumptions for most readers, and the name collision makes it harder than it needs to be to build a more correct mental model.
> 
I can tell you that every single time I've mentioned the __class__ nonlocal in a StackOverflow answer, someone mistakenly "corrected" me to tell me either "you need to write self.__class__" or "that won't work because it's the runtime type of self, which could be a subclass".

I think a better solution, if you were doing it from scratch, might be to get rid of the __class__ member and force people to use type(self) to get it, so TOOTDI. (Of course that implies that code that needs to change __class__ would have to call some function to do so, but I'm not sure that's a bad thing. Dynamically changing your type is a rare thing to do in Python, and unusual or impossible in most other OO languages, except maybe during initialization in the Smalltalk family, so why shouldn't it be signaled by something scary?)

On the other hand, is __class__ the only thing that's spelled with double underscored that isn't an attribute? Maybe that's the part that's confusing people...
> It would actually be possible to still make that transition, with a view to deprecating the current "__class__" spelling at some point in the distant future. I'm too close to the problem to judge whether or not it would actually help reduce confusion, though.
> 
> Regards,
> Nick.
> 
> >
> > Cheers,
> > Nick.
> 
> _______________________________________________
> 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/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150309/1f77dbb8/attachment.html>


More information about the Python-ideas mailing list