Kanwar Saad
Kanwar Saad

Reputation: 2329

substitute of function pointers in python

I have worked in low level C programming for years and I don't have enough exposure to Object oriented approaches. In C if I was developing some layered architecture then each layer has interfaces defined by function pointers. The advantage of that the whole layer can be replaced by just setting those function pointers at initialization to another layer.

I want the same thing but this time in Python. What is the coolest way to achieve that. To give a little background to my problem, I have a data generator which can output records to different mediums. The medium is specified at the configuration time. I don't want to use if or switch statements here. The best way was to use function pointers in C but what are the options available here in Python. Any Object oriented approaches are also appreciated.

Thanks

Upvotes: 9

Views: 14292

Answers (4)

John La Rooy
John La Rooy

Reputation: 304355

You can simply put the functions in a dict

{"type1": function1,
 "type2": function2,
 "type3": function3,
}.get(config_option, defaultfunction)(parameters, go, here)

default_function is called if none of the keys match

If you wish you can separate out the selection an the calling

selected_function = {"type1": function1,
                     "type2": function2,
                     "type3": function3,
                     }.get(config_option, defaultfunction)

some_instance = SomeClass(selected_function)

Upvotes: 5

Greg Hewgill
Greg Hewgill

Reputation: 993901

Python supports functions as a first-class data type. So you can do something like:

def foo(x):
    print("foo: " + x)

def bar(x):
    print("bar: " + x)

f = foo
f("one")
f = bar
f("ten")

prints

foo: one
bar: ten

This is very similar to your experience with function pointers in C. Although Python certainly supports more elaborate object-oriented programming styles, you are under no obligation to use them.

An example using classes, where you can group related functions together:

class Dog:
    def noise(self, x):
        print("bark! " + x)
    def sleep(self):
        print("sleeping on floor")

class Cat:
    def noise(self, x):
        print("meow! " + x)
    def sleep(self):
        print("sleeping on keyboard")

a = Dog()
a.noise("hungry")
a.sleep()

a = Cat()
a.noise("hungry")
a.sleep()

This version prints:

bark! hungry
sleeping on floor
meow! hungry
sleeping on keyboard

Upvotes: 16

Facundo Casco
Facundo Casco

Reputation: 10605

Maybe something alog this lines:

class MediumA:
    def one_method(self):
        return

    def another_method(self):
        return

class MediumB:
    def one_method(self):
        return

    def another_method(self):
        return


class DataGenerator:
    # here you can read this from some configuration
    medium = MediumA()  # this will be shared between all instances of DataGenerator

    def generate(self):
        # all mediums responds to this methods
        medium.one_method()
        medium.another_method()

generator = DataGenerator()
# you can even change the medium here
generator.medium = MediumB()

Hope it helps

Upvotes: 3

argentage
argentage

Reputation: 2778

in python functions are first-class data types.

def z(a):
    print(a)
def x(a):
    print "hi"

functions = [z,x]
y = functions[0]
y("ok") # prints "ok"
y = functions[1]
y("ok") # prints "hi"

Upvotes: 3

Related Questions