9

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

1

4 Answers 4

16

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
Sign up to request clarification or add additional context in comments.

3 Comments

+1. It might be worth adding an example showing how easily you can port the "table of function pointers" C idiom (which is where I think the OP is going) to a simple dict of functions.
Thanks, this looks nice ... but can you elaborate other options or programming styles to achieve this goal.
@KanwarSaad: I have added an example using classes.
5

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)

Comments

3

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"

1 Comment

Note that "first class value" means much more than function pointers: For example, new functions can be created at runtime.
3

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.