简体   繁体   中英

passing class name as argument to function?

Here in given code it is passing class name ie 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!!

    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(

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

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.

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.

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.

yes you can pass to class args or function parameter

1 - using type(object)

2 - class Name

==>>passing ClassA to to ClassB and fname as parameters

class ClassA(object):

    def __init__(self,):

        pass



class ClassB(object):

    def __init__(self, arg):
        print(type(arg))
        pass


def fname(arg):
    print(type(arg))
    pass
valueA: ClassA = ClassA()

ClassB(type(valueA))
ClassB(ClassA)

fname(type(valueA))
fname((ClassA))

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