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






 
Udemy APAC

Udemy APAC








Online Live Training

We provide online live training on a wide range of technologies for working professionals from Corporate. We also provide training for students from all streams such as Computer Science, Information Technology, Electrical and Mechanical Engineering, MCA, BCA.

Courses Offered :

  • C Programming
  • C++ Programming
  • Data Structure
  • Core Java
  • Python
  • Java Script
  • Advance Java (J2EE)
  • Hibernate
  • Spring
  • Spring Boot
  • Data Science
  • JUnit
  • TestNg
  • Git
  • Maven
  • Automation Testing - Selenium
  • API Testing

NOTE: The training is delivered in full during weekends and during the evenings during the week, depending on the schedule.

If you have any requirements, please send them to prowessapps.in@gmail.com or 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