理解类内对python类实例方法的调用
考虑以下课程:尤其是理解类内对python类实例方法的调用,python,Python,考虑以下课程:尤其是 __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 cluster
__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
在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
但这两种方法:
K
最大迭代次数
都以同样的方式声明。为什么在调用时,它们最终会受到不同的对待?对我来说很好:
p = PowerClustering(10,30)
In [723]: p.maxIterations
Out[723]: 30
In [724]: p
Out[724]: <class 'PowerClustering'>: k=10 maxIterations=30
我在OP中尝试了你的版本除了int到max_迭代之外,你还传递了什么吗?
p = PowerClustering(10,30)
In [723]: p.maxIterations
Out[723]: 30
In [724]: p
Out[724]: <class 'PowerClustering'>: k=10 maxIterations=30