考虑以下课程:尤其是
__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
repr ()中的不起作用:
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
但这两种方法都是:
以相同的方式声明。为什么他们在被调用时最终会被区别对待?
答案 0 :(得分:0)
对我来说很好:
p = PowerClustering(10,30)
In [723]: p.maxIterations
Out[723]: 30
In [724]: p
Out[724]: <class 'PowerClustering'>: k=10 maxIterations=30
我在OP中尝试了你的版本