access function in python module outside of class

I have the next situation. I try to overwrite method send_error in BaseHTTPRequestHandler in BaseHTTPServer.py file. BaseHTTPServer.py has such structure concerning send_error method:

def _quote_html(html):
    blah
    blah

class HTTPServer():
    blah
    blah
class BaseHTTPRequestHandler():
    blah
    blah
    def send_error(self):
        blah
        blah
        content = (self.error_message_format %
                {'code': code, 'message': _quote_html(message), 'explain': explain})

Here inside send_error method _quote_html function is called. It works inside BaseHTTPServer.py file but if create my own httphandler, inherit from BaseHTTPRequestHandler and try to overwrite send_error, my function send_error can not access _quote_html function located in BaseHTTPServer.py file outside of BaseHTTPRequestHandler class:

Traceback (most recent call last):
  File "/usr/lib/python2.7/SocketServer.py", line 593, in process_request_thread
    self.finish_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/python_codes/Junior/Level1/C&C/HttpServer/HttpHandler.py", line 12, in __init__
    BaseHTTPRequestHandler.__init__(self, *args, **kwargs)
  File "/usr/lib/python2.7/SocketServer.py", line 649, in __init__
    self.handle()
  File "/usr/lib/python2.7/BaseHTTPServer.py", line 340, in handle
    self.handle_one_request()
  File "/python_codes/Junior/Level1/C&C/HttpServer/HttpHandler.py", line 26, in handle_one_request
    if not self.parse_request():
  File "/usr/lib/python2.7/BaseHTTPServer.py", line 286, in parse_request
    self.send_error(400, "Bad request syntax (%r)" % requestline)
  File "/python_codes/Junior/Level1/C&C/HttpServer/HttpHandler.py", line 106, in send_error
    {'code': code, 'message': _quote_html(message), 'explain': explain})
NameError: global name '_quote_html' is not defined 

So my question is how can I access function outside parent class in module file? In my case from send_error() to _quote_html(). Everythong is imported from BaseHTTPServer.py:

from BaseHTTPServer import *

Upvotes: 0

Views: 216

Answers (2)

Luis González
Luis González

Reputation: 3559

I have tried this and it works fine:

def b():
    return 45
class Person(object):

    def getYears(self):
        return self.b()

print Person().getYears()

So, the only difference I see is the 'objects' between brackets.

Upvotes: 0

Dmitry Nedbaylo
Dmitry Nedbaylo

Reputation: 2334

Rename your function into quote_html. Or do explicit import like this:

from BaseHTTPServer import _quote_html

Since:

from M import * does not import objects whose name starts with an underscore.

Upvotes: 1

Related Questions