函数的可选参数组 - python

时间:2015-04-24 15:37:28

标签: python class init

好的,所以我必须制作两个类(在两个不同的脚本中),都叫做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

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

2 个答案:

答案 0 :(得分:2)

您必须检查传递给函数的参数并采取相应的行动。通常,您要做的是选择一个规范表示来实际存储数据,并__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

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

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

答案 1 :(得分:0)

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

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

的代码