Tuesday, November 22, 2011

Lambda Expressions in Python


Below is just a sample piece of code that I was playing with the other day, it was fun to mess with and interesting to see how the scope of the variables declared for the lambda expressions behaved. Mainly, that sometimes it seemed that variables declared within lambda expressions were accessible outside of what I thought their scope should be. This happened with the lambda expressions containing lambda expressions both expressions taking variables. It was a fun case study that got me away from all this serious project stuff I'm working on. And I figured I would share, so without further ado, here is the code: throw it into your favorite python IDE (command line, IDLE, eclipse) and run the show function and then play with it yourself. :)


#LAMBDA EXPRESSION TESTING
#Can you horribly abuse lambda expressions such that
#you dont have to actually create functions in a class?


class LamClass1:
    def __init__(self,x,y,z):
        self.x = x
        self.y = y
        self.z = z
        self.getX = lambda: self.x
        self.getY = lambda: self.y
        self.getZ = lambda: self.z
        self.norm = lambda a: a/(self.x+self.y+self.z)
        self.xyLine = lambda m: (lambda n: n*m*self.x+self.y)
        self.xzLine = lambda m: m*self.x+self.z
        #nesting functions to return functions
        self.makeLine = lambda m: lambda b:  lambda x: (m*x+b)
        #nesting arguments!
        self.nesting = lambda m,b,x: (((self.makeLine(m))(b))(x))
        #tupling it up
        self.listF = lambda a,b,n: a+b,a*b,a-b,b-a,n
        #This is a tuple you access to access a function how cool!
        self.listT = lambda a,b: lambda n: (a+b)**n,lambda a,b: lambda n: (a-b)**n
        self.shallowCopy = lambda: self
        self.deepCopy = lambda: LamClass1(self.x,self.y,self.z)
        self.tupleReturn = lambda a,b: (a,b)
        self.funcList = lambda: (self.getX,self.getY,self.getZ,self.xyLine)
        #you cant do multiple statements inside a lambda
        #but you could nest everyline of code as a lambda being called and such


def show():
    l = LamClass1(2,3,4)
    #Standard gets but with lambda
    l.getX()
    l.getY()
    l.getZ()
    #An example of argument passing
    f=l.xyLine(2)(3)
    #Multi Function Computation:
    f=l.listF[0](2,2,3)
    #Example of how Scope Changes
    f=l.listF[1]
    print 'Global Scope of lambda variables'
    print a,b,n
    #you could write a =1 and then another print and watch it be strange
    #Lambda Expression Variables are global, this could be bad
    #Example of tupled functions
    f=l.listT[0](1,2)(2) #(a+b)^n
    f=l.listT[1](2,3)(3) #(a-b)^n
    print a,b,n
    #Notice how I have never declared a,b,n
    #an example of shallowCopying and mutability
    print 'Shallow Copy'
    L2 = l.shallowCopy()
    print 'l.x=',l.getX()
    print 'L2.x=',L2.getX()
    l.x = 3
    print 'l.x = 3'
    print 'L2.x=',L2.getX()
    #an example of deep copy
    L3 = l.deepCopy()
    print 'Deep Copy'
    print 'l.x=',l.getX()
    print 'L3.x=',L3.getX()
    l.x = 5
    print 'l.x =',l.x
    print 'L3.x=',L3.getX()
    print 'Function List'
    print 'l.funcList()[0]() -> getX()'
    print l.funcList()[0]()
    print 'l.funcList()[1]() -> getY()'
    print l.funcList()[1]()
    print 'l.funcList()[2]() -> getZ()'
    print l.funcList()[2]()
    print 'l.funcList()[3](2)(3) -> xyLine(2)(3)'
    print l.funcList()[3](2)(3)
    

No comments:

Post a Comment