2015-04-02 30 views
0

考慮下面的類:特別是瞭解調用

__repr__() 

方法:

class PowerClustering(object): 

def __init__(self, kClusters, max_iterations=20, init_mode="random"): 
    self.k_ = kClusters 
    self.max_iterations = max_iterations 

@property 
def k(self): 
    """Get the number of clusters.""" 
    return self.k_ 

@property 
def maxIterations(self): 
    """Get the number of clusters.""" 
    return self.max_iterations 

def __repr__(self): 
    return "%s: k=%d maxIterations=d " %(self.__class__, self.k) 

這工作得很好:

def __repr__(self): 
    return "%s: k=%d maxIterations=%d" %(self.__class__, self.k, self.maxIterations) 

test_pic output: <class 'pyspark.mllib.clustering.PowerClustering'>: k=2 

但添加在致電

self.maxIterations 
再版()

不起作用:

def __repr__(self): 
    return "%s: k=%d maxIterations=%d" %(self.__class__, self.k, self.maxIterations) 

這裏現在是輸出:

Traceback (most recent call last): 
    in __repr__ 
    return "%s: k=%d maxIterations=%d" %(self.__class__, self.k, self.maxIterations) 
TypeError: %d format: a number is required, not JavaMember 

但兩者的那些方法:

  • ķ
  • maxIterations

以相同的方式聲明。爲什麼最終在被調用時被區別對待?

+0

你通過別的東西,除了一個'int'到'max_iterations'的OP試圖您的版本? – salparadise 2015-04-02 06:16:31

回答

0

正常工作:

p = PowerClustering(10,30) 
In [723]: p.maxIterations 
Out[723]: 30 

In [724]: p 
Out[724]: <class 'PowerClustering'>: k=10 maxIterations=30