python's OOP question

neoedmund neoedmund at gmail.com
Mon Oct 16 04:29:18 CEST 2006


I'm trying to achieve a higher level of "reusability". Maybe it cannot
be done in python? Can anybody help me?


Ben Finney wrote:
> "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