Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

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 *

Answer*

Draft saved
Draft discarded
Cancel
3
  • I second the explicit import suggestion. from <module> import * is generally frowned upon since it pollutes the local namespace and makes it hard to track an item's origin. Commented Jan 30, 2015 at 12:32
  • @ВладХристенко Though... its fine even to accept... but please do both - "upvote and accept" the right answer. Commented Jan 30, 2015 at 12:42
  • I currently do not have enough reputation to upvote((( I would do it. Commented Jan 30, 2015 at 12:46

lang-py

AltStyle によって変換されたページ (->オリジナル) /