python's OOP question

Ben Finney bignose+hates-spam at benfinney.id.au
Sun Oct 15 22:22:54 EDT 2006


"neoedmund" <neoedmund at gmail.com> writes:

> 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()

Setting attributes on an object externally isn't the same thing as
making bound methods of that object.

In this case, 'o.m' is a bound method of a C2 instance, and has no
knowledge of C1. 'o.v' is a bound method of a C1 instance, and has no
knowledge of C2. Neither of them has any knowledge of C3.

What is it you're trying to achieve?

-- 
 \     "Unix is an operating system, OS/2 is half an operating system, |
  `\       Windows is a shell, and DOS is a boot partition virus."  -- |
_o__)                                                  Peter H. Coffin |
Ben Finney




More information about the Python-list mailing list