Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中函数指针的替换_Python - Fatal编程技术网

python中函数指针的替换

python中函数指针的替换,python,Python,我在低级C编程领域工作多年,对面向对象的方法没有足够的了解。在C语言中,如果我正在开发某种分层体系结构,那么每一层都有由函数指针定义的接口。这样做的好处是,只需在初始化时将这些函数指针设置到另一层,就可以替换整个层 我想要同样的东西,但这次是Python。实现这一目标最酷的方法是什么。为了给我的问题提供一点背景知识,我有一个数据生成器,可以将记录输出到不同的介质。在配置时指定介质。我不想在这里使用if或switch语句。最好的方法是在C中使用函数指针,但是Python中有哪些可用的选项呢。也欢迎

我在低级C编程领域工作多年,对面向对象的方法没有足够的了解。在C语言中,如果我正在开发某种分层体系结构,那么每一层都有由函数指针定义的接口。这样做的好处是,只需在初始化时将这些函数指针设置到另一层,就可以替换整个层

我想要同样的东西,但这次是Python。实现这一目标最酷的方法是什么。为了给我的问题提供一点背景知识,我有一个数据生成器,可以将记录输出到不同的介质。在配置时指定介质。我不想在这里使用if或switch语句。最好的方法是在C中使用函数指针,但是Python中有哪些可用的选项呢。也欢迎任何面向对象的方法


谢谢

Python支持函数作为一级数据类型。因此,您可以执行以下操作:

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

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

f = foo
f("one")
f = bar
f("ten")
印刷品

foo: one
bar: ten
这与您在C中使用函数指针的经历非常相似。尽管Python当然支持更复杂的面向对象编程风格,但您没有义务使用它们

使用类的示例,可以将相关函数分组在一起:

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()
此版本打印:

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

在python中,函数是一流的数据类型

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"

也许是这样的:

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()

希望它有帮助

您可以简单地将函数放在dict中

{"type1": function1,
 "type2": function2,
 "type3": function3,
}.get(config_option, defaultfunction)(parameters, go, here)
如果所有键都不匹配,则调用默认函数

如果您愿意,可以将所选内容与呼叫内容分开

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

some_instance = SomeClass(selected_function)

看看多态性+1。可能值得添加一个示例,说明如何轻松地将“函数指针表”C习惯用法(我认为OP就是要将其移植到一个简单的
dict
函数中。谢谢,这看起来不错。。。但是你能详细说明其他选项或编程风格来实现这个目标吗?@KanwarSaad:我添加了一个使用类的示例。请注意,“第一类值”的含义远不止函数指针:例如,可以在运行时创建新函数。