有没有办法在python中的用户定义的类上定义list(obj)方法?

时间:2015-05-31 06:45:09

标签: python python-3.x

可以实现方法__str__(self)来返回字符串。是否有__list__(self)个对应方? (__list__无效!) 是.toList()的方法吗?

1 个答案:

答案 0 :(得分:15)

没有直接的对应物,但list()是通过迭代参数来实现的。因此,请实施the iterator protocol__iter__())或bound indexing__len__()__getitem__())。