python's OOP question

Kay Schluehr kay.schluehr at gmx.net
Mon Oct 16 04:21:50 CEST 2006


neoedmund wrote:
> There's a program, it's result is "unexpected aaa", i want it to be
> "expected aaa". how to make it work?
>
> [code]
>
> class C1(object):
> 	def v(self, o):
> 		return "expected "+o
>
> class C2(object):
> 	def v(self, o):
> 		return "unexpected "+o
> 	def m(self):
> 		print self.v("aaa")
>
> class C3(object):
> 	def nothing(self):
> 		pass
>
> def test1():
> 	o = C3()
> 	setattr(o,"m",C2().m)
> 	setattr(o,"v",C1().v)
> 	o.m()
>
> test1()
>
> [/code]

class C3(C1, C2):pass

>>> C3.mro()  # shows method resolution order
[<class '__main__.C3'>, <class '__main__.C1'>, <class '__main__.C2'>,
<type 'object'>]

>>> o = C3()
>>> o.m()
expected aaa




More information about the Python-list mailing list