Optimization


In this chapter we are going to discuss few ideas which can be used to optimize & enhance the performance of the python code.

Most of the ideas present below are nothing but good common sense.


In [ ]:


In [5]:
d = 10

def B():
    d = 20
    def C():
        d = 30
        return d
    print(d)
    return C

x = B()
print(x)


20
<function B.<locals>.C at 0x0000000005DE2B70>

In [3]:
d = 10

def B():
    d = 20
    def C():
        global d
        d = 30
        return d
    return C
a = B()
print(a())
print(d)


30
30

In [6]:
d = 10

def B():
    d = 20
    def C():
        nonlocal d
        print(d)
        d = 30
        return d
    return C
a = B()
print(a())
print(d)


20
30
10

In [ ]: