What is the semantics meaning of 'object'?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun Jun 23 01:23:05 EDT 2013


On Sat, 22 Jun 2013 22:27:10 -0600, Ian Kelly wrote:

> On Sat, Jun 22, 2013 at 9:20 PM, Steven D'Aprano
> <steve+comp.lang.python at pearwood.info> wrote:
>> * on the down side, automatic delegation of special double-underscore
>> methods like __getitem__ and __str__ doesn't work with new-style
>> classes.
> 
> I actually consider that an up side.  Sure it's inconvenient that you
> can't delegate all such methods at once just by overriding
> __getattribute__, but it would be more troublesome to *accidentally*
> implement such methods because you implemented __getattribute__.  

It's hard to see a case where that would be a bad thing.

1) If the proxied object doesn't include __dunder__ methods, then the 
proxy will just end up up calling the default object dunder methods, 
exactly as if they weren't proxied.

2) If the proxied object does include dunders, then you generally want 
the proxy to call them, with perhaps one or two exceptions, which need to 
be overridden regardless of whether they are dunders or not.


> And
> then there are methods that really should not be delegated in the first
> place, like __del__.

If you're using __del__, you're probably doing something wrong :-)

I suppose that __del__ is a good counter-example, but (1) hardly any 
classes use __del__, and (2) for those that do, it's *way* simpler to 
manually override __del__ in the proxy than to manually delegate every 
dunder method you care about. There are typically a lot of dunder methods 
you care about.

It is not the case that dunder methods cannot be automatically proxied 
because somebody deliberately designed Python to work that way. It's an 
accidental side-effect of the way new-style classes resolve method calls, 
due to decisions made for other reasons having nothing to do with 
delegation. That this accident just happens to have one tiny silver 
lining doesn't change the fact that, overall, it's still a dark cloud.

In classic classes, automatic delegation was a first-class design 
pattern. With new-style classes, it's second-class, and that's a pity.


-- 
Steven



More information about the Python-list mailing list