dunder-docs (was Python is DOOMED! Again!)

Ethan Furman ethan at stoneleaf.us
Tue Feb 3 18:55:14 EST 2015


On 02/03/2015 03:32 PM, Marko Rauhamaa wrote:
> Gregory Ewing <greg.ewing at canterbury.ac.nz>:
> 
>> You seem to be suggesting an optimisation that pre-creates bound
>> methods when the instance is created. Keeping a cache of bound methods
>> would achieve the same thing without changing the semantics. I think
>> CPython might already be doing that, but I'm not sure.
> 
> No, I'm saying Python should behave differently.
> 
> Python:
> 
>    >>> class A:
>    ...    def f(self):
>    ...       print("f")
>    ...    def g(self):
>    ...       print("g")
>    ... 
>    >>> a = A()
>    >>> a.__class__.f = a.__class__.g
>    >>> a.f()
>    g
>     
> In my preferred semantics, a.f() would print
> 
>    >>> a.f()
>    f

That is, as you acknowledge, not Python.

Thankfully, it will also never be Python.

However, because Python is so awesome, you can twist your own code to behave that way, to a point: simply have your
__init__ ( or __new__) populate the instance dict with all non-dunder methods.

Or even better, implement your own proto(mumble) type stack in Python, so there is some warning that your instances
don't behave quite like normal Python instances.

--
~Ethan~

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20150203/721e62f0/attachment.sig>


More information about the Python-list mailing list