Python列表方法
我必须用Python进行文本处理 所以我创建了一个类Python列表方法,python,list,function,call,Python,List,Function,Call,我必须用Python进行文本处理 所以我创建了一个类TextProcessing: manipulator = TextProcessing("one two three four four") repr(manipulator) => "one two three four four" manipulator["four"] //=> [3, 4] => I made a list for that 但在那之后,我不得不做一个这样的函数: manipulator.two.
TextProcessing
:
manipulator = TextProcessing("one two three four four")
repr(manipulator) => "one two three four four"
manipulator["four"] //=> [3, 4] => I made a list for that
但在那之后,我不得不做一个这样的函数:
manipulator.two.replace("second") => "one second three four four"
我不知道如何将这两个链接到我的列表中。您可以使用覆盖 或
感谢您的帮助我还有一个问题:如何使用getattr获取多个属性?恐怕没那么容易
class A(object):
def __init__(self, s):
self.s = s
def replace(self, x):
self.s = self.s.replace(self.x, x)
def __getattr__(self, name):
self.x = name
return self
a = A('abc')
a.b.replace('X')
print a.s
class B(object):
def __init__(self, x, y):
self.x = x
self.y = y
def replace(self, x):
self.x.s = self.x.s.replace(self.y, x)
class A(object):
def __init__(self, s):
self.s = s
def __getattr__(self, name):
return B(self, name)
a = A('abc')
a.b.replace('X')
print a.s