我想写一个继承地图类的自定义类。
class mapT(map):
def __init__(self,iii):
self.obj = iii
但我无法初始化它。
# Example init object
ex = map(None,["","1","2"])
exp1 = mapT(ex)
# TypeError: map() must have at least two arguments.
exp1 = mapT(None,ex)
# TypeError: __init__() takes 2 positional arguments but 3 were given
如何在python中创建一个继承地图的类?或者为什么我不能在 python 中继承地图?
===== 添加 =====
我想要实现的是为可迭代对象添加自定义方法
def iterZ(self_obj):
class iterC(type(self_obj)):
def __init__(self,self_obj):
super(iterC, self).__init__(self_obj)
self.obj = self_obj
def map(self,func):
return iterZ(list(map(func,self.obj))) # I want to remove "list" here, but I can't. Otherwise it cause TypeError
def filter(self,func):
return iterZ(list(filter(func,self.obj))) # I want to remove "list" here, but I can't. Otherwise it cause TypeError
def list(self):
return iterZ(list(self.obj))
def join(self,Jstr):
return Jstr.join(self)
return iterC(self_obj)
这样我就可以做到这一点:
a = iterZ([1,3,5,7,9,100])
a.map(lambda x:x+65).filter(lambda x:x<=90).map(lambda x:chr(x)).join("")
# BDFHJ
而不是这个:
"".join(map(lambda x:chr(x),filter(lambda x:x<=90,map(lambda x:x+65,a))))