Is it possible (and wise) to extend the None-type ?
Bruno Desthuilliers
bruno.42.desthuilliers at websiteburo.invalid
Thu Nov 27 05:17:02 EST 2008
Stef Mientki a écrit :
> <snip>
>> Alternative (if you *really* want to save the explicit test) is to
>> attach the behaviour modification to the *relevant* class:
>>
>> class NonEvent(Event):
>> def do_nothing(self):
>> pass
>> skip = jump = hop = waltz = saunter = do_nothing
>> def __len__(self):
>> return 0
>> NON_EVENT = NonEvent()
>> del NonEvent
>>
>> # later:
>>
>> def amethod(self, event=NON_EVENT):
>> if event: # still works thanks to __len__ above
>> event.skip()
>> # now can avoid test, if desired
>> event.skip()
>>
>>
> thanks guys, the Null, Dummy, Skipper, Nonevent class works great
The NullObject pattern is indeed a proven solution. Another one is to
use a decorator:
def skip_event(event_handler):
def wrapper(self, event=None):
if event is not None:
event.skip()
return event_handler(self, event)
wrapper.__name__ = "skip_event_%s" % event_handler.__name__
wrapper.__doc__ = event_handler.__doc__
return wrapper
@skip_event
def on_menu_file_open(self, event=None):
do_something_usefull_here
More information about the Python-list
mailing list