2013-11-01 24 views
0

我不確定這是否是正確的方式來提出問題,但在這裏。如何傳遞隱式參數

我有一個類說

class T_shape(Shape): 
    def __init__(self, center): 
     coords = [Point(center.x - 1, center.y), 
        Point(center.x,  center.y), 
        Point(center.x + 1, center.y), 
        Point(center.x,  center.y + 1)] 
    Shape.__init__(self, coords, 'yellow') 
    self.center_block = self.blocks[1] 

此類已被其他人編碼的,我只是想問問什麼是傳遞參數的正確方法。中心是這種情況是像(3,4)的元組。但是當我嘗試以這種方式直接傳遞它時,它說'元組'對象沒有屬性'x'。

任何幫助,將不勝感激。

+6

能通過一項[collections.namedtuple](http://docs.python.org/3/library/collections.html#collections.namedtuple)(代替指示要做什麼的任何其它文檔。 ..) –

回答

2

我不確定什麼樣的對象center是或T_shape構造函數期望的? 但是你可以用namedtuple來實現。

from collections import namedtuple 
center = namedtuple('center', ['x', 'y'], verbose=True) 
center = center(x=3,y=4) 
t_shape = T_Shape(center) 
+0

謝謝你解決了這個問題。 – Strommer