In [4]:
def func():
return 1
In [5]:
func()
Out[5]:
In [6]:
s = 'this is a global variable'
def func():
print locals()
In [7]:
print globals()['s']
In [8]:
func()
In [9]:
def hello(name = 'rustom'):
return 'Hello ' + name
In [10]:
hello()
Out[10]:
In [11]:
greet = hello
In [12]:
greet
Out[12]:
In [13]:
greet()
Out[13]:
In [14]:
# del hello
In [15]:
greet
Out[15]:
In [16]:
greet()
Out[16]:
In [17]:
hello()
Out[17]:
In [18]:
greet2 = hello()
In [19]:
greet2
Out[19]:
In [20]:
greet2()
In [21]:
del hello
In [22]:
greet2
Out[22]:
In [23]:
greet
Out[23]:
In [24]:
greet()
Out[24]:
In [25]:
def welcome(name = 'Prithvi'):
print 'inside welcome()'
def namaste():
return '\t inside the namaste()'
def hariom():
return '\t inside hariom()'
if name == 'Prithvi':
return namaste
else:
return hariom
In [26]:
x = welcome('potter')
In [27]:
print x()
In [28]:
def hello():
return 'Namaste Prithvi!!'
In [29]:
def other(func):
print 'other code goes here'
print func()
In [30]:
other(hello)
In [31]:
def new_decorator(func):
def wrap_func():
print 'code here....before executing the func'
func()
print 'code here....after executing the func'
return wrap_func
In [32]:
def func_needs_decorator():
print 'This function needs a decorator!!'
In [33]:
func_needs_decorator()
In [34]:
m_func_needs_decorator = new_decorator(func_needs_decorator)
In [35]:
m_func_needs_decorator
Out[35]:
In [36]:
m_func_needs_decorator()
In [37]:
@new_decorator
def another_funcNeedsDecorator():
print 'Another function needing decorator'
In [38]:
another_funcNeedsDecorator
Out[38]:
In [39]:
another_funcNeedsDecorator()
In [44]:
repr(x)
Out[44]:
In [45]:
str(x)
Out[45]:
In [ ]:
In [ ]:
In [ ]: