Here in given code it is passing class name i.e. MyRequestHandler to TCP and also after taking class name as argument what does it do with that.So my question is that can class name be used as argument and also class name doesn't refer to anything so how is it possible???i apologize for silly ques!!
在给定的代码中,它将类名称即MyRequestHandler传递给TCP,并且在将类名称作为参数后,它对它做了什么。所以我的问题是类名可以用作参数,类名也不是指任何东西怎么可能???我为愚蠢的问题道歉!
from SocketServer import (TCPServer as TCP,
StreamRequestHandler as SRH)
from time import ctime
HOST = ''
PORT = 21567
ADDR = (HOST, PORT)
class MyRequestHandler(SRH):
def handle(self):
print '...connected from:',self.client_address
self.wfile.write('[%s] %s' % (ctime(),
self.rfile.readline()))
tcpServ = TCP(ADDR, MyRequestHandler)
print 'waiting for connection...'
tcpServ.serve_forever(
3 个解决方案
#1
11
Absolutely you can pass a class name as an argument to a function:
绝对可以将类名作为参数传递给函数:
>>> class a():
... def __init__(self):
... print "an object from class a is created"
...
>>> def hello(the_argument):
... x = the_argument()
...
>>> hello(a)
an object from class a is created
>>>
#2
3
You aren't passing in the name of a class, you are passing in a reference to the class. A class is an object just like everything else. Think of it as a function that returns another object. You can pass a class as an argument just like you can pass a function, a string, or any other object.
您没有传递类的名称,而是传递对类的引用。类就像其他所有类一样是一个对象。将其视为返回另一个对象的函数。您可以将类作为参数传递,就像传递函数,字符串或任何其他对象一样。
As for what the called function can do with it -- it create create instances of that class. In this case, the called function doesn't really care what class it uses, as long as it implements a particular interface. So, the called function doesn't ever see the name of the class, it's just told how to create an instance of it (by virtue of being given a reference to the class)
至于被调用函数可以用它做什么 - 它创建该类的创建实例。在这种情况下,被调用的函数并不真正关心它使用哪个类,只要它实现了特定的接口即可。因此,被调用的函数永远不会看到类的名称,它只是被告知如何创建它的实例(由于给出了对类的引用)
In the case of a server, it needs to create a new instance of some object for every connection to the server. So, you give it the class you want it to use, and it creates instances for each connection. This lets the server create the objects when it needs them, rather than requiring you to create them ahead of time.
对于服务器,它需要为服务器的每个连接创建一个对象的新实例。因此,您为它提供了您希望它使用的类,并为每个连接创建实例。这使服务器可以在需要时创建对象,而不是要求您提前创建它们。
#3
0
can class name be used as argument? Yes. but in your code you are not passing a class name to the TCP constructor, you are passing a request handler to the constructor.
可以使用类名作为参数吗?是。但是在您的代码中,您没有将类名传递给TCP构造函数,而是将请求处理程序传递给构造函数。
also class name doesn't refer to anything so how is it possible? As mention above, you are passing a request handler to the Tcp constructor, your request handler refers to an action which TCP server will use to handle the incoming request. So it does refer to something.
类名也没有提到任何东西,所以怎么可能呢?如上所述,您将请求处理程序传递给Tcp构造函数,您的请求处理程序引用TCP服务器将用于处理传入请求的操作。所以它确实指的是某些东西。
#1
11
Absolutely you can pass a class name as an argument to a function:
绝对可以将类名作为参数传递给函数:
>>> class a():
... def __init__(self):
... print "an object from class a is created"
...
>>> def hello(the_argument):
... x = the_argument()
...
>>> hello(a)
an object from class a is created
>>>
#2
3
You aren't passing in the name of a class, you are passing in a reference to the class. A class is an object just like everything else. Think of it as a function that returns another object. You can pass a class as an argument just like you can pass a function, a string, or any other object.
您没有传递类的名称,而是传递对类的引用。类就像其他所有类一样是一个对象。将其视为返回另一个对象的函数。您可以将类作为参数传递,就像传递函数,字符串或任何其他对象一样。
As for what the called function can do with it -- it create create instances of that class. In this case, the called function doesn't really care what class it uses, as long as it implements a particular interface. So, the called function doesn't ever see the name of the class, it's just told how to create an instance of it (by virtue of being given a reference to the class)
至于被调用函数可以用它做什么 - 它创建该类的创建实例。在这种情况下,被调用的函数并不真正关心它使用哪个类,只要它实现了特定的接口即可。因此,被调用的函数永远不会看到类的名称,它只是被告知如何创建它的实例(由于给出了对类的引用)
In the case of a server, it needs to create a new instance of some object for every connection to the server. So, you give it the class you want it to use, and it creates instances for each connection. This lets the server create the objects when it needs them, rather than requiring you to create them ahead of time.
对于服务器,它需要为服务器的每个连接创建一个对象的新实例。因此,您为它提供了您希望它使用的类,并为每个连接创建实例。这使服务器可以在需要时创建对象,而不是要求您提前创建它们。
#3
0
can class name be used as argument? Yes. but in your code you are not passing a class name to the TCP constructor, you are passing a request handler to the constructor.
可以使用类名作为参数吗?是。但是在您的代码中,您没有将类名传递给TCP构造函数,而是将请求处理程序传递给构造函数。
also class name doesn't refer to anything so how is it possible? As mention above, you are passing a request handler to the Tcp constructor, your request handler refers to an action which TCP server will use to handle the incoming request. So it does refer to something.
类名也没有提到任何东西,所以怎么可能呢?如上所述,您将请求处理程序传递给Tcp构造函数,您的请求处理程序引用TCP服务器将用于处理传入请求的操作。所以它确实指的是某些东西。