# -*- coding: utf-8 -*-
'''
This is a class used for finding all characters that a str contains.
The return value is a list.
'''
class super_str(str):
"""add support for findall()"""
def __init__(self, arg):
super(super_str, self).__init__()
self.body = arg
def findall(self, arg, start=0):
body = self.body
result = []
while True:
pos = body.find(arg, start)
if pos >= 0:
result.append(pos)
start = pos + len(arg)
#body = body[pos+len(arg):]
continue
break
return result
实例
注意返回值是一个list,代表查找字符位置
s = super_str('nihaoa')
print s.findall('a')
<<<output:[3,5]