Function Pointer in Python.


In python, everything is object, everything means everything.

If we create a variable of any type like int, float, str etc, that is also an object.

So, functions are also objects in python.

As we know that every object has its own id, so we can access and store that id to a variable.

Example:

# let's careate a function

def myfun():
    print("Hello from function")

# call the function
myfun()

# print the id of function
print(id(myfun))

# access the id and store in varaible

fun = myfun  notice, not using paranthesis

''' now, this fun is pointing to the function myfun(),
means, we can access and execute the function by
calling fun allso'''

print('accessing through fun')
fun()
                
OUTPUT
Hello from function
46541528
accessing through fun
Hello from function
                

In the above program, fun is function pointer, that is pointing to a function i.e. myfun().

Above code clearly showing that we can store and access the function from another variable. The variable which points to a function is called function pointer.

def myfun():
    print("Hello from myfun")


def myfun1(arg1):
    print("I accept one argument arg1 :", arg1)

f = myfun
f1 = myfun1

f()
f1("Hello")  
OUTPUT
Hello from myfun
I accept one argument arg1 : Hello
                    

Next chapter is Anonymous Function






 








Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in


CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-8527238801 , +91-9451396824

© 2017, prowessapps.in, All rights reserved