Sometimes you want a Class whose functions do nothing.
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.
|
Sign in to comment