oop - Python: How to distinguish between inherited methods -


newbie python question. have class inherits several classes, , of specialization classes override methods base class. in cases, want call unspecialized method. possible? if so, what's syntax?

class base(object):     def foo(self):         print "base.foo"      def bar(self):         self.foo()  # can force call base.foo if foo has override?   class mixin(object):     def foo(self):         print "mixin.foo"   class composite(mixin, base):     pass   x = composite() x.foo()  # executes mixin.foo, perfect x.bar()  # indirectly executes mixin.foo, want base.foo 

you can make call want using syntax

base.foo(self) 

in case:

class base(object):     # snipped      def bar(self):         base.foo(self)  # call base.foo regardless of if subclass                         # overrides  # snipped   x = composite() x.foo()  # executes mixin.foo, perfect x.bar()  # prints "base.foo" 

this works because python executes calls bound methods of form

instance.method(argument) 

as if call unbound method

class.method(instance, argument) 

so making call in form gives desired result. inside methods, self instance method called on, i.e, implicit first argument (that's explicit parameter)

note if subclass overrides bar, there's nothing (good) can afaik. that's way things work in python.


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -