简体   繁体   中英

How do I activate (execute) methods of a class in Python?

I have a program ( simple web server ) which I try to understand. There is a class called MyHandler. In this class we define 2 methods do_GET and do_POST.

I do not understand several things:

  1. Where do we use the two above defined methods? I would expect to see something like that objectname.do_GET() and objectname.do_POST() but I do not see such things.

  2. At which point we instantiate the class? Do we use the whole class (not an instance of the class) as an argument to the HTTPServer?

  3. Why do we specify argument in the definition of the class (BaseHTTPRequestHandler) and then do not use it?

I'm quite new to Python, but I will have a go at an answer—it might help me learn too!

  1. We don't ever call the do_GET() and do_POST() methods from our code, this is done automatically by the HTTPServer class instance when GET and POST requests are made (see point 2).

  2. The HTTPServer will create an instance of the MyHandler class for each HTTP request that is made.

  3. BaseHTTPRequestHandler is not an argument: it specifies that our MyHandler class subclasses BaseHTTPRequestHandler .

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM