[英]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
另一種方法是使用類方法來提供備用構造函數。
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()的代碼,...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.