Is it possible (and wise) to extend the None-type ?

S

Stef Mientki

hello,

I've the idea that I always have a lot of useless code in my programs,
like the next example.

def _On_Menu_File_Open ( self, event = None ):
if event :
event.Skip ()

instead of

def _On_Menu_File_Open ( self, event = None ):
event.Skip ()

So I would like to extend the None-type (if that's possible),
with a dummy Skip() method.

Is it wise to do ?
If not what are the disadvantages ?

thanks,
Stef Mientki
 
J

John Machin

hello,

I've the idea that I always have a lot of useless code in my programs,
like the next example.

  def _On_Menu_File_Open ( self, event = None ):
    if event :
     event.Skip ()

instead of

  def _On_Menu_File_Open ( self, event = None ):
   event.Skip ()

So I would like to extend the None-type (if that's possible),
with a dummy Skip() method.

Is it wise to do ?
No.

If not what are the disadvantages ?

Assume that as well as your Event class, you also have a Foo class
with a dothis method and a Bar class with a dothat1 and a dothat2
method: you would need to extend the None-type (if that's possible) to
have dummy skip, dothis, dothat1 and dothat2 methods. Each time you
invent a new class, you need to consider adding one or more new dummy
methods to your extended None-type.

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()

HTH ... BTW, ever heard of PEP 8?

Cheers,
John
 
S

Stef Mientki

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
HTH ... BTW, ever heard of PEP 8?
I guess Python is missing some kind of CSS ;-)

cheers,
Stef
 
B

Bruno Desthuilliers

Stef Mientki a écrit :
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top