user3398557
user3398557

Reputation: 481

passing class name as argument to function?

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

    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(

Upvotes: 45

Views: 146475

Answers (4)

ceremcem
ceremcem

Reputation: 4350

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

Upvotes: 70

Mhmoud Sabry
Mhmoud Sabry

Reputation: 403

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

Upvotes: 0

user116541
user116541

Reputation: 106

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.

Upvotes: 0

Bryan Oakley
Bryan Oakley

Reputation: 385830

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.

Upvotes: 19

Related Questions