简体   繁体   English

在Python中级联可选参数

[英]Cascading optional parameters in Python

I have a series of classes which can be created by optionally passing a parameter, or if ommited, it will use a default data structure. 我有一系列的类,可以通过有选择地传递参数来创建,或者如果省略,它将使用默认的数据结构。 All the classes work in the same way, thus: 所有的类都以相同的方式工作,因此:

class square(object):
    _vertices = [ (0, 0), (0, 10), (10, 10), (10, 0) ]
    def __init__(self, vertices=_vertices):
        # more stuff ...

class triangle(object):
    _vertices = [ (0, 0), (5, 10), (10, 10) ]
    def __init__(self, vertices=_vertices):
        # more stuff ...

# ... more classes

I also have a factory function which takes a string and creates an appropriate object 我还有一个工厂函数,该函数接受一个字符串并创建一个适当的对象

def create_shape(key)
    if key == 'square':
       return square()
    elif key == 'triangle':
       return triangle()

Though naturally, this does not allow me to pass the vertices parameter 虽然自然,但这不允许我传递vertices参数

shape1 = create_shape('square') # OK, basic creation
shape2 = create_shape('triangle', my_own_vertices) # Not OK, param 2
                                                   # can't go through factory

My way round this was to embellish the factory as follows, although, to me, this seems a very clumsy way of going about things. 我的方法是按如下方式修饰工厂,尽管对我来说,这似乎是一件很笨拙的事情。

def create_shape(key, vertices=None):
    if key == 'square':
        return square(vertices) if vertices else square()
    elif key == 'triangle':
        return triangle(vertices) if vertices else triangle()

In essence, I simply want to pass through an optional parameter without the top level ( create_shape ) knowing what the default should be . 本质上, 我只是想通过一个可选参数,而顶层( create_shape )却不知道默认值是什么 Is there a clearer / simpler / more pythonic way of doing this? 有更清晰/更简单/更pythonic的方法吗? I've looked at other questions / answers but nothing seemed much better than the above. 我看过其他问题/答案,但似乎没有什么比上述更好。

If you want pass optional parameters to your function, you can use **kwags 如果要将可选参数传递给函数,可以使用** kwags

def create_shape(key, **kwargs):
    if key == 'square':
        return square(**kwargs)
    elif key == 'triangle':
        return triangle(**kwargs)

Then, you can call your factory function like this. 然后,您可以像这样调用工厂函数。

create_shape('triangle', vertices = [( 0,0), (1, 10)])

The init function of your Triangle class would receive the new vertices values. Triangle类的init函数将接收新的顶点值。

You could change your shape classes to have None as a default parameter and handle the None case in your constructor. 您可以更改形状类以将None作为默认参数,并在构造函数中处理None情况。

class square(object):
    _vertices = [ (0, 0), (0, 10), (10, 10), (10, 0) ]
    def __init__(self, vertices=None):
        if not vertices: 
            vertices = _vertices
        self.vertices = vertices

Then you can just use the default parameter in the factory class: 然后,您可以在工厂类中使用默认参数:

def create_shape(key, vertices=None):
    if key == 'square':
        return square(vertices)
    elif key == 'triangle':
        return triangle(vertices)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM