分配关键字参数的最佳方法?

时间:2018-11-12 14:53:13

标签: python keyword-argument

在被调用的函数之间分配关键字参数的认可编程模式是什么?

考虑这个人为(和越野车)示例:

conditionalPanel(condition = 'input.disclaimerweg == ""',
             absolutePanel(id = "disclaimer", class = "panel panel-default", fixed = TRUE,
                           draggable = FALSE, top = 41, left = 0, right = 0, bottom =  0,
                           width = "auto", height = "auto",
                           br(),
                           #actionButton("demoversie", "Demoversie"),
                           br(),
                           h1("Disclaimer!"),
                           actionButton("disclaimerweg", "Akkoord"),
                           br(),
                           br()

             )
)

  conditionalPanel(condition = 'input.disclaimerweg != ""',
  # Input: Select a file ----
  fileInput("fileUploaded", "Choose CSV File",
            multiple = FALSE,
            accept = c("text/csv",
                       "text/comma-separated-values,text/plain",
                       ".csv"))
)

很显然,def create_box(**kwargs): box = Box() set_size(box, **kwargs) set_appearance(box, **kwargs) def set_size(box, width=1, height=1, length=1): ... def set_appearance(box, material='cardboard', color='brown'): ... 方法将反对接收set_size()material关键字参数,就像color将反对接收set_appearance(),{{1} }或width参数。

有一个有效的论点,height应该使所有关键字和默认值都显式显示,但显而易见的实现相当笨拙:

length

有没有更Python化的方法来解决这个问题?

3 个答案:

答案 0 :(得分:4)

您可以将**kwargs作为函数的最后一个参数添加,否则这些函数会很烦人。

def create_box(**kwargs):
    box = Box()
    set_size(box, **kwargs)
    set_appearance(box, **kwargs)

def set_size(box, width=1, height=1, length=1, **kwargs):
    ...

def set_appearance(box, material='cardboard', color='brown', **kwargs):
    ...

答案 1 :(得分:3)

假设您无法更改set_sizeset_appearance,则可以根据预期的keword参数过滤kwargs,但是我不确定这是否是最有效的方法。 ..

import inspect

def create_box(**kwargs):
    box = None
    set_size_args = inspect.getargspec(set_size).args
    set_size(box, {k:v for k,v in kwargs.items() if k in set_size_args})
    set_appearance_args = inspect.getargspec(set_appearance).args
    set_appearance(box, {k:v for k,v in kwargs.items() if k in set_appearance_args})


def set_size(box, width=1, height=1, length=1):
    pass

def set_appearance(box, material='cardboard', color='brown'):
    pass

create_box(width=1, height=1, length=1, material='', color='')

(对于Python 3使用inspect.signature()

答案 2 :(得分:1)

嗯,就我个人而言,我将传递一个包含我需要的所有参数的对象。这样一来,您只会一直传递一个参数。

示例:

class BoxDefinition:
    def __init__(self, width=1, height=1, length=1, material='cardboard', color='brown'):
        self.width = width 
        self.height = height
        # and so on for the rest of the params ...

在每种方法的内部,您都可以从该对象读取

def create_box(box_definition):
    box = Box()
    set_size(box, box_definition)
    set_appearance(box, box_definition)

def set_size(box, box_definition):
    width = box_definition.width

def set_appearance(box, box_definition):
    color = box_definition.color

然后您可以这样称呼它:

definition = BoxDefinition(width=2, height=3, length=4)
create_box(definition)

实际上,您甚至可以创建BoxDefinition和Box类的属性,然后您唯一需要传递的就是Box。