Class extension confusion :(

r0g aioe.org at technicalbloke.com
Thu Nov 11 03:56:58 EST 2010


On 10/11/10 09:52, Peter Otten wrote:
> class PlainAJAXRequestHandler(BaseHTTPRequestHandler):
>      def __init__(self, api_call, paths, *args, **kw):
>          BaseHTTPRequestHandler.__init__(self, *args, **kw)
>          self.api_call = api_call
>          self.paths = paths


Hmm, the plot thickens! I always thought you had to call the parent 
constructor first (as above) when extending a constructor (not that I've 
had occasion to do that in a long time), but it turns out when I do this 
the lines below it never get executed and when I move them above that 
line they seem to work fine so it appears I was wrong about that. I've 
tried typing many variants of "python class extend constructor" into 
google over the last few days but I'm damned if I can find the docs 
explaining this. I'm sure I found them several years back when I first 
too up python, maybe by google-fu is on the wane!

Anyway, that's not my main question, this is... The extra names that I 
pass to functools.partial seem to be bound permanently into the 
namespace of my class now i.e. I can reference them as 'api_call' and 
'paths' anywhere in the classes' methods as opposed to having to assign 
them in the constructor and reference them as 'self.api_call' and 
'self.paths'. I'm not 100% how that's working but in practical terms it 
suggests to two lines assigning those names to data attributes are 
redundant as I can access them anywhere anyway. Indeed, I've commented 
them out and my app still seems to work fine so...

Question A) Are there any good reasons why I shouldn't just do that? 
(other than B!)

Question B) The only reason I can think of so far is that I don't have a 
clear picture of how those names came to end up in that scope, it seems 
very convenient but I'm worried it's black magic of some sort! Could 
anyone explain or point me to the right docs please?

Please shout if you would like to see the source.

Thanks muchly :)


Roger.





More information about the Python-list mailing list