Welcome, guest | Sign In | My Account | Store | Cart

Sometimes you want a Class whose functions do nothing.

Python, 19 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# You can insert the folowing two lines 
# to your program to make NullClass available.

class NullClass:
   def __getattr__(self,name): return lambda *x,**y: None

# -------------------------------------------
# Example: switched writing to a logfile

log=err=NullClass()
if verbose:
   log = open('/tmp/log')
   err = open('/tmp/err')

log.write('blabla')
err.write('blabla error')

#This obviously avoids the usual pollution from stuff like "if verbose: ".
#NullClass also accepts keyword arguments.  
Created by H. Krekel on Wed, 3 Apr 2002 (PSF)
Python recipes (4591)
H. Krekel's recipes (4)

Required Modules

  • (none specified)

Other Information and Tasks