创建具有多个选项的Python函数的最佳方法? [英] Best way to create Python function with multiple options?

查看:99
本文介绍了创建具有多个选项的Python函数的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了很多自定义函数,发现其中很多包含一些相同或相似的代码块(例如,仅包含略有不同的字符串或参数).像这样:

I've defined a bunch of custom functions and find a lot of them include some identical or similar blocks of code (e.g. just including slightly different strings or arguments). So something like:

def func1(a, b, c):
    some_identical_code
    some_similar_code
    more_identical_code
    some_unique_code
    final_similar_code

# similarly for func2, func3...

我想尝试将它们组合成一个带有附加设置"参数的函数,这是因为这些函数显然是相关的,并且使代码更紧凑.显而易见但不优雅的流程是这样的:

I would like to try to combine these into a single function that takes an additional 'setting' argument, both because the functions are obviously related and to make the code more compact. The obvious but inelegant flow for this would be something like:

def func(a, b, c, setting):
    # setting = '1', '2', etc.

    some identical code

    if setting == '1':
        similar_code_1
    elif setting == '2':
        similar_code_2
    # etc. for other settings options

    more_identical_code

    if setting == '1':
        unique_code_1
        final_similar_code_1
    elif setting == '2':
        unique_code_2
        final_similar_code_2
    # etc. for other settings options

我该如何更优雅地进行此类操作?有标准的方法吗?

我正在尝试的一个选项是使用函数中内置的词典来解析相似的代码块:

One option I'm trying is to use dictionaries built into the function to parse the similar code blocks:

def func(a, b, c, setting):
    # setting = '1', '2', etc.
    simdict = {'1': '_1', '2': '_2' ...}

    some identical code
    similar_code(simdict[setting])
    more_identical_code

    if setting == '1':
        unique_code_1
    elif setting == '2':
        unique_code_2
    # etc. for other settings options

    final_similar_code(simdict[setting])

这有帮助,但是1)我不确定它是否是好的设计,以及2)仅在完全相同,相似或唯一的代码相互穿插时,它才有很大帮助.

This helps but 1) I'm not sure if it's good design and 2) it only helps so much when the identical vs. similar vs. unique code is very interspersed.

推荐答案

摘要!

  • 相同的代码应该是一个函数;
  • 类似的代码应该是带有参数的函数;
  • 唯一代码应该是每个函数的唯一功能;
  • 面向对象的代码可以在这里提供帮助.

要了解这个主意:

def identical_code():
    lorem ipsum

def similar_code(parameter):
    lorem ipsum

class Base:
    def __call__(self, a, b, c):
        identical_code()
        similar_code(self.parameter())
        unique_code()
    def unique_code(self):
        NotImplemented
    def parameter(self):
        NotImplemented

class Func_1(Base):
    def unique_code(self):
        do_it_1()
    def parameter(self):
        return 1

class Func_2(Base):
    def unique_code(self):
        do_it_2()
    def parameter(self):
        return 2

然后您可以呼叫Func1()(a, b, c)Func_2()(a, b, c).

这里的NotImplemented方法用于显示基类仅用于定义常见行为,但是如果您未指定这两种方法,则实际上无法执行任何操作.

Here the NotImplemented methods are there to show that the base class is just there to define the common behavior, but can't really do anything if you don't specify those two methods.

某些更Python化的方法可能是只使用鸭子类型并删除继承项:

Some maybe more pythonic way would be to just duck type and remove inheritance:

def func(a, b, c, setting):
    identical_code()
    similar_code(setting.parameter())
    setting.unique_code()

class Func_1:
    def unique_code(self):
        do_it_1()
    def parameter(self):
        return 1

class Func_2:
    def unique_code(self):
        do_it_2()
    def parameter(self):
        return 2

def func1(a, b, c):
    return func(a, b, c, Func_1)

def func2(a, b, c):
    return func(a, b, c, Func_2)

这篇关于创建具有多个选项的Python函数的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆