On 11/11/10 09:34, Peter Otten wrote:
r0g wrote:

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?

Python looks for attributes in the instance first, and then in the class as
a fallback. You only need to put them in the instance if you expect that you
want a different value for every instance.

Peter



Thanks Peter, I think I understand now, I'll quickly explain my picture of things though so you or some other denizens of the mighty comp.lang.python can correct me if I'm wrong!...

I can access the parameters I pass to __init__ within the classes' methods without using self. as these methods are run within the constructor itself and are therefore within it's local scope.

That also explains why I had to call the constructor _after_ creating the new data attributes to have them be included. My initial belief that one has to call the parent constructor as the first action in the extended constructor is not technically valid, in most cases it can be called at any point but in situations like the above it can mess things up so I ought to put it at the end of my constructors for it to always work. Are there any drawbacks to calling it last or is that how it is supposed to work?

The BaseHTTPRequestHandler seemed curiously constructed to me at first too but I think I can see why now... as it's a handler it's only ever meant to be a transient thing and it requires no external parameters (unless you're me!) as it's always called by HTTPServer which has all the info necessary so there's be no sense requiring users to create an instance then call a method to get the servers response, you may as well just bundle it all into one. Also as it might be called hundreds or thousands of times in quick succession it would be important to have it finish and get garbage collected.

I also see that I ought to at least assign these extended params to data attributes and access them via self. like I would normally do as I can't guarantee that implementation of BaseHTTPREquestHandler will remain constant over time. If I understand correctly it may also be possible (and more efficient) to use setattr() to inject the parameters I want into the class as class attributes before use, rather than assigning them to data attributes every time I instantiate an new instance.

Actually looking at the code from BaseRequestHandler it seems I may have overlooked the proper way of associating a callback function anyway. It looks like I should just define self.handle() in my subclass so that's one less parameter to worry about, although I think I may still need to use the factory function method you showed me to get the "paths" dictionary in there.

Thanks so much for all your help, I really appreciate it and, assuming I'm not totally wrong about all of the above, I find it reassuring that I'm not going mad!

Cheers,

Roger.
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to