Question

Consider the following case:

class Meta(type):
    def shadowed(cls):
        print "Meta.shadowed()"
    def unshadowed(cls):
        print "Meta.unshadowed()"

class Foo(object):
    __metaclass__ = Meta

    def shadowed(self):
        print "Foo.shadowed()"

I can call get the bound method unshadowed on Foo and it works fine:

>>> Foo.unshadowed
<bound method Meta.unshadowed of <class '__main__.Foo'>>
>>> Foo.unshadowed()
Meta.unshadowed()

However, I can't seem to get the bound method shadowed on Foo - it directs me rather to the unbound method which must be called with instances of Foo:

>>> Foo.shadowed
<unbound method Foo.shadowed>
>>> Foo.shadowed()

Traceback (most recent call last):
  File "<pyshell#45>", line 1, in <module>
    Foo.shadowed()
TypeError: unbound method shadowed() must be called with Foo instance as first argument (got nothing instead)

Is there any way to get <bound method Meta.shadowed of <class '__main__.Foo'>>?

Was it helpful?

Solution

It seems one potential answer (maybe not the best) is found in this answer on how to bind unbound methods. So we can do this:

>>> Meta.shadowed.__get__(Foo, Meta)()
Meta.shadowed()

Better demonstration:

class Meta(type):
    def shadowed(cls):
        print "Meta.shadowed() on %s" % (cls.__name__,)
    def unshadowed(cls):
        print "Meta.unshadowed() on %s" % (cls.__name__,)

class Foo(object):
    __metaclass__ = Meta

    def shadowed(self):
        print "Foo.shadowed()"

class Bar(object):
    __metaclass__ = Meta

Bar.unshadowed()                   #Meta.unshadowed() on Bar 
Bar.shadowed()                     #Meta.shadowed() on Bar
Foo.unshadowed()                   #Meta.unshadowed() on Foo
#Foo.shadowed()                    #TypeError    
Meta.shadowed.__get__(Foo, Meta)() #Meta.shadowed() on Foo
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top