Python List methods -
i had make text processing in python.
so created class textprocessing
:
manipulator = textprocessing("one 2 3 4 four") repr(manipulator) => "one 2 3 4 four" manipulator["four"] //=> [3, 4] => made list
but after had make function that:
manipulator.two.replace("second") => "one second 3 4 four"
i have no idea how link 2 here list.
you can play overriding __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('abc') a.b.replace('x') print a.s
or
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('abc') a.b.replace('x') print a.s
Comments
Post a Comment