Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #-------------------------------------------------------------------------------
- # Name: module1
- # Purpose:
- #
- # Notes: One of the major pieces of Python that lambda examples are applied to regularly are
- # Tkinter callbacks.
- # Basically, Pythonβs lambda is a tool for building functions (or more precisely,
- # function objects). That means that Python has two tools for building functions: def and lambda.
- #
- # References: http://www.blog.pythonlibrary.org/2010/07/19/the-python-lambda/
- # http://pythonconquerstheuniverse.wordpress.com/2011/08/29/lambda_tutorial/
- # http://www.bogotobogo.com/python/python_functions_lambda.php
- #
- # Author: User
- #
- # Created: 17/05/2014
- # Copyright: (c) User 2014
- # Licence: <your licence>
- #-------------------------------------------------------------------------------
- # Imports #
- import math
- # Main #
- def main():
- # Variables
- # Local
- #Global
- print(">>>Best if code is viewed in PyScripter. All code is directly from the above references,\
- a modification of their code or inspired by their code.<<<") %()
- print
- # Lambda in a 'jump tabel' for actually referencing a lambda func in a list
- print(">>>Using lambda funcs from a list, a 'jump table'<<<")
- print(">>>I.e. you can select an expression from a list based on some criteria. Cool!<<<") %()
- print("Create expression list and reference and use based on list index value")
- k_LambdaTest = [lambda x: x ** 2, lambda x: x ** 3, lambda x: x ** 4]
- print("Here we cycle thru each lambda in the list and print results")
- for i in range(len(k_LambdaTest)): ## Uses length of lambda object to set up iteration
- print("Lambda func %i is %i") %(i, k_LambdaTest[i](3)) ## Cycles thru each lambda func with 3 and prints result
- print("Here we only ref the first lambda in list and print results.")
- print("First lambda func result is: %i") %k_LambdaTest[0](3) ## Calls the first lambda func in list with 3
- print
- #------------------------------
- # Functions #
- # Normal Functions
- # Generator Functions
- # Lambda Functions
- # Classes #
- # Main Loop #
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement