Python 将类名作为参数传递给函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28885018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:51:58  来源:igfitidea点击:

passing class name as argument to function?

python

提问by user3398557

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(

回答by ceremcem

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
>>> 

回答by Bryan Oakley

You aren't passing in the nameof a class, you are passing in a referenceto 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.

在服务器的情况下,它需要为与服务器的每个连接创建某个对象的新实例。所以,你给它你想要它使用的类,它为每个连接创建实例。这让服务器在需要时创建对象,而不是要求您提前创建它们。

回答by user116541

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 服务器将用于处理传入请求的操作。所以它确实指的是某事。