Chapter 6: Preprocessor directives

Program Source Code 6.1, page no: 169

In [2]:
def concat(a):           #macro expansion
    return str(a)

def foot(a):          #macro expansion
    return str(a)

p='sport'
b=foot(p)

#Result
exec("print %s")%concat('b')
sport

Program Source Code 6.2, page no: 172

In [3]:
import sys
import os

approot=os.path.dirname(os.path.abspath(sys.argv[0]))       #to include file
def line():
    line=1
    line=2
    return line

print approot,
#x-    #no macro as such can be defined in python
#y+      #no macro as such can be defined in python
C:\Users\nita\Documents\IPython Notebooks

Program Source Code 6.3, page no: 173

In [7]:
import inspect

True=1
False=0
approot=os.path.dirname(os.path.abspath(sys.argv[0]))            #filename shown

def lineno():                      #to print lineno
   return inspect.currentframe().f_back.f_lineno
    
if __debug__:                  #assertion
    print 'assertion error line',lineno(),'file(',os.path.dirname(os.path.abspath(sys.argv[0])),')'
    
  
assert(True)
i=0
assert(__debug__)
assertion error line 11 file( C:\Users\nita\Documents\IPython Notebooks )
In [ ]: