Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > My stupidity / strange inconsistency overriding class methods

Reply
Thread Tools

My stupidity / strange inconsistency overriding class methods

 
 
andrew cooke
Guest
Posts: n/a
 
      04-19-2011
Hi,

I've been staring at this problem, in various forms, all day. Am I missing something obvious, or is there some strange hardwiring of isinstance? This is with Python 3.2.

class A(metaclass=ABCMeta):
@classmethod
def __instancecheck__(cls, instance): return False
# no override
assert isinstance(A(), A)
assert A.__class__.__instancecheck__(A, A())

class B(type):
def foo(self): return 42
class C(metaclass=B):
@classmethod
def foo(cls): return 7
# override
assert C().__class__.foo() == 7

It seems to me that the above two cases are inconsistent. ABCMeta declares __instancecheck__ just like B declares foo. Yet C can override foo, but A is unable to override the instance check.

Please help!

Thanks,
Andrew
 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      04-20-2011
On Tue, Apr 19, 2011 at 4:52 PM, andrew cooke <> wrote:
> Hi,
>
> I've been staring at this problem, in various forms, all day. Â*Am I missing something obvious, or is there some strange hardwiring of isinstance? Â*This is with Python 3.2.
>
> Â* Â* Â* Â*class A(metaclass=ABCMeta):
> Â* Â* Â* Â* Â* Â*@classmethod
> Â* Â* Â* Â* Â* Â*def __instancecheck__(cls, instance): return False
> Â* Â* Â* Â*# no override
> Â* Â* Â* Â*assert isinstance(A(), A)
> Â* Â* Â* Â*assert A.__class__.__instancecheck__(A, A())


[You've already figured out the issue, but since I spent a while
composing this, and for the benefit for the archives, I'll post
anyway.]

Makes sense after a little thought.
http://docs.python.org/reference/dat...ubclass-checks
"Note that [ __instancecheck__() is ] looked up on the type
(metaclass) of a class. [It] cannot be defined as [a classmethod] in
the actual class. This is consistent with the lookup of special
methods that are called on instances, only in this case the instance
is itself a class."

Recall from http://docs.python.org/reference/dat...-style-classes
that lookup of __special__ methods never consults instance
dictionaries, instead skipping directly to the type's namespace; as
the quote says, in this case, the instance (of ABCMeta) is itself a
class/type (namely A). Your two assert statements are therefore almost
precisely equivalent in this case; and since the latter involves
A.__class__ (a.k.a. ABCMeta) rather than A itself, it's understandable
that that A's namespace is not consulted.

> Â* Â* Â* Â*class B(type):
> Â* Â* Â* Â* Â* Â*def foo(self): return 42
> Â* Â* Â* Â*class C(metaclass=B):
> Â* Â* Â* Â* Â* Â*@classmethod
> Â* Â* Â* Â* Â* Â*def foo(cls): return 7
> Â* Â* Â* Â*# override
> Â* Â* Â* Â*assert C().__class__.foo() == 7


More simply: assert C.foo() == 7

"foo" is not a __special__ method name; therefore we look in the
instance dictionary of the receiver (i.e. C) before consulting the
receiver's type (i.e. B). Our check in the instance dictionary is
successful (we find C.foo), and therefore we don't even bother looking
at C's type (i.e. B, where we would find B.foo).

> It seems to me that the above two cases are inconsistent. Â*ABCMeta declares __instancecheck__ just like B declares foo. Â*Yet C can override foo, but A is unable to override the instance check.


The difference is in the __special__-ness of the method names in question.

Cheers,
Chris
--
http://blog.rebertia.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
Overriding class methods Vasileios Zografos C++ 2 03-19-2007 12:30 PM
Overriding class methods Vasileios Zografos C++ 0 03-15-2007 03:36 PM
Junit: overriding original class private methods Robert M. Gary Java 1 12-29-2006 01:37 AM
Assigning methods to objects, and assigning onreadystatechange to an XMLHttpRequest -- an inconsistency? weston Javascript 1 09-22-2006 09:33 AM



Advertisments