imageguy wrote:
I have an object the I would like to use as a base class. Some of the
methods I would like to override completely, but others I would simply
like to call the base class method and use the return value in the
child method. The purpose here is to eliminate the duplication of
valuable code in the parent, when I really just need the child to
operate of a results of the parent.
Consider the following two classes;
class Parent(object):
def process(self, value):
retval = "Parent.result('%s')" % value
return retval
class Child(Parent):
def __init__(self):
Parent.__init__(self)
def process(self, value):
retval = "Child.result('%s')" % super(Child, self).process
(value)
return retval
So ....
foo = Child()
print foo.process('the value')
Child.result('Parent.result('the value')')
IS there another pattern or idiom that would accomplish this?
This seems a bit 'smelly' to me. Also seems almost the inverse of
decorators, but I am not sure decorators would be appropriate in this
case.
Any help suggestions would be appreciated.
g.
Just don't replace the methods you want to use from the superclass
-- it really is that simple. Notice how str is changed below,
but not repr:
class GroupedInt(int):
def __str__(self):
old = super(GroupedInt, self).__str__()
first = old[: len(old) % 3 or 3]
if first == '-':
first = old[: 4]
parts = [first]
parts.extend(old[n : n + 3] for n in
range(len(first), len(old), 3))
return '_'.join(parts)
for n in 1, 12, 123, 1234, 12345, 123456, 1234567:
print '%s\t%s=%r\t%s=%r' % (n, GroupedInt(n), GroupedInt(n),
GroupedInt(-n), GroupedInt(-n))
1 1=1 -1=-1
12 12=12 -12=-12
123 123=123 -123=-123
1234 1_234=1234 -1_234=-1234
12345 12_345=12345 -12_345=-12345
123456 123_456=123456 -123_456=-123456
1234567 1_234_567=1234567 -1_234_567=-1234567
--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list