Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing class name as argument to function?

Tags:

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( 
like image 627
user3398557 Avatar asked Mar 05 '15 18:03

user3398557


People also ask

Can we pass class as function arguments?

Passing and Returning Objects in C++ In C++ we can pass class's objects as arguments and also return them from a function the same way we pass and return other variables. No special keyword or header file is required to do so.

How do you pass a class name as an argument in Python?

yes of coarse you can pass classes or functions or even modules ... def foo(): pass here it gives it address for ex. function foo at0x024E4463 means it is given address of foo function but in class itdoesnt means class foo: pass here it gives <class.

Can we pass class name as parameter in Java?

You can pass the class as parameter.

How can we pass a function name as an argument to another function?

To pass a function as an argument to another function, write the name of the function without parenthesis in the function call statement (just like what we do with variables) and accept the reference of the function as a parameter in the called function.


2 Answers

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 >>>  
like image 140
ceremcem Avatar answered Oct 20 '22 17:10

ceremcem


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.

like image 35
Bryan Oakley Avatar answered Oct 20 '22 19:10

Bryan Oakley