What If..... Strong Types
Peter Hansen
peter at engcorp.com
Wed Jun 19 08:36:25 EDT 2002
Don Garrett wrote:
>
> Peter Hansen wrote:
> > Preventing modifying methods on an instance would make some of my
> > automated testing really difficult, if not impossible.
>
> Hum... that's the only specific example I've ever run across where someone
> would need to switch out a method implementation at run time in production
> code. Depickling and several other very nice features probably depend on it,
> but they are core libraries, not general code. I draw a distinction there.
>
> I suggest that you could create a testing subclass that overrides the
> methods in questions with testing versions of the methods.
>
> Would that work as well? As easily?
I don't think it would in all cases, although I'm sad to admit I
didn't actually think of it before. Python is just too darned accessible
in this way. :-)
Since we're testing the code sort of "in situ", I cannot always ensure
that the objects created would be of my subclass without changing other
parts of the code. Think of an factory object embedded inside something
else, and I'm trying to test that one of the items returned responds in
the proper fashion by dynamically replacing one of its methods which is
called by another method which I'm going to call.
Were the code written in a beautiful, elegant, highly decoupled
fashion in the first place I think what you suggest would work. Since
it's not, the very pragmatic aspects of Python that allow me to do
what I do are very helpful. I don't believe any highly static language
would let me do this sort of thing without great inconvenience.
-Peter
More information about the Python-list
mailing list