函数的可选参数组 - python

Groups of optional arguments for a function - python

好的,所以我必须制作两个 类(在两个不同的脚本中),都称为 Block,它存储有关矩形块的位置和大小的信息。版本 1 应具有存储块中心坐标(作为单独的 x 和 y 坐标或作为一对*数字)以及块的宽度和高度的属性。版本 2 应该有属性来存储左下角("SW" 角)和右上角("NE" 角)的坐标。

所以我知道如何分别为每个版本设置构造函数,但是对于这个分配,两个版本都应该有一个构造函数,它将采用一对中心坐标以及宽度和高度(作为浮动点数),或表示块的任意两个对角的两对坐标。这是我到目前为止尝试过的:

class Block:
    """Stores information about the position and size of a rectangular block.

    Attributes: x-coordinate (int), y-coordinate (int), width (int), height (int) OR northeast corner (int) and southwest corner (int)"""

    def __init__(self, center = '', width = '', height = '', SW = '', NE = ''):
        """A constructor that assigns attributes to the proper variables

        Block, tuple, tuple -> None"""
        self.center = center
        self.width = width
        self.height = height
        self.SW = SW
        self.NE = NE

但我很确定这实际上并没有按照我想要的方式工作。基本上我需要能够输入一组变量作为中心、宽度和高度,或者我需要输入两个角。有办法吗?

您必须检查向函数传递了哪些参数并采取相应的行动。通常,您想要做的是选择一种规范表示来实际存储数据,并让 __init__ 将传递给它的任何参数转换为规范形式。例如:

# Use None to represent missing data. Think about it: "hello" is not a 
# valid width; neither is "".
def __init__(self, center=None, width=None, height=None, SW=None, NE=None):
    """A constructor that assigns attributes to the proper variables

    Block, tuple, tuple -> None"""

    if center is not None and width is not None and height is not None:
        # If either SW or NE is given, ignore them 
        self.center = center
        self.width = width
        self.height = height
    elif SW is not None and NE is not None:
        # _convert_corners is a helper function you define
        self.center, self.width, self.height = _convert_corners(SW, NE)
    else:
        # Not entirely true. Give width, height, and one corner, you
        # could reconstruct the center, but this is just an example.
        raise ValueError("Insufficient information to construct Block")

您可以使用属性来即时计算其他属性,而不是冗余地存储它们:

@property
def SW(self):
    # return the south-west corner as computed from
    # self.center, self.height, and self.width

@property
def NE(self):
    # return the north-east corners computed from
    # self.center, self.height, self.width

另一种方法是使用 class 方法来提供备用构造函数。

def __init__(self, center, width, height):
    "Define a block by its center, width, and height"
    self.center = center
    self.width = width
    self.height = height

@classmethod
def from_corners(self, sw, ne):
    "Define a block by two corners"
    c, w, h = _convert_corners(sw, ne)
    return Block(c, w, h)

正在使用:

# For demonstration purposes, I'm assuming points like the center
# and the corners are simple tuples of integer coordinates
b1 = Block((10, 50), 5, 7)
b2 = Block.from_corners((20, 30), (40, 70))

你快到了。尝试这样的事情...

class Block:
    def __init__(self, center = '', width = '', height = '', SW = '', NE = ''):
        if SW != ''  or  NE != '':
            if SW == ''  and  NE ==  '':   # usage error
                return None                # throw an exception here
            self.center = getCenterFromCorners(SW, NE)  # ((sw[0]+ne[0])/2, ...)
            self.width = getWidthFromCorners(SW, NE)    # abs(sw[0]-ne[0])
            self.height = getHeightFromCorners(SW, NE)  # abs(sw[1]-ne[1])
        else:
            if center == ''  or  width == ''  or '' height == '':
                return None                # throw exception
            self.center = center
            self.width = width
            self.height = height
        return self

# usage: block1 and block2 should be similar
block1 = Block(center=(10,20), height=2, width=4)
block2 = Block(SW=(9,18), NE=(11,22))

我相信您可以替换 getCenterFromCorners() 的代码,...