method = Klass.othermethod considered PITA

John J. Lee jjl at pobox.com
Sun Jun 5 11:05:06 EDT 2005


Jeff Epler <jepler at unpythonic.net> writes:

> On Sat, Jun 04, 2005 at 10:43:39PM +0000, John J. Lee wrote:
> > 1. In derived classes, inheritance doesn't work right:
> 
> Did you expect it to print 'moo'?  I'd have been surprised, and expected
> the behavior you got.

Me too.  It's at the time of *writing* the code, before the subclass
even exists, that the problem can arise, if you're not thinking about
derivation.  Sure, if you're not thinking about derivation, you have
many other problems when somebody starts deriving from you, but, well,
here's another issue to remember.

Fine once you've noted this particular wrinkle, I suppose.

And, looking again at another use case (in urllib2, the http_error_30*
methods) I guess it's true that there are cases where methods are
really distinct but merely happen to have the same implementation, so
that method2 = method1 is actually what you want, hence better than a
trivial method definition.


> > 2. At least in 2.3 (and 2.4, AFAIK), you can't pickle classes that do
> >    this.
> 
> In all the versions of Python I've used, classes are pickled by name.
> This example you wrote doesn't pose any special problem when pickling.
> 
> >>> pickle.dumps(A)
> 'c__main__\nA\np0\n.'
> >>> pickle.dumps(B)
> 'c__main__\nB\np0\n.'

I meant class instances.


John



More information about the Python-list mailing list