Overriding member methods in __init__
Duncan Booth
duncan.booth at invalid.invalid
Mon Dec 3 10:20:37 EST 2007
c james <cjames at callone.net> wrote:
> Thanks, I was trying to eliminate another level of indirection with a
> test at each invocation of __call__
>
>
Try using different subclasses for each variant:
class YesNo(object):
def __new__(cls, which, *args, **kw):
if cls is YesNo:
if which:
return object.__new__(Yes)
else:
return object.__new__(No)
def __init__(self, which, *args, **kw):
print "New", self.__class__, which, args, kw
def __call__(self, val):
raise NotImplementedError()
class Yes(YesNo):
def __call__(self, val):
print 'Yes', val
class No(YesNo):
def __call__(self, val):
print 'No', val
>>> y = YesNo(True)
New <class '__main__.Yes'> True () {}
>>> y('hello')
Yes hello
>>> n = YesNo(False)
New <class '__main__.No'> False () {}
>>> n('hello')
No hello
>>>
More information about the Python-list
mailing list