gaining deeper understanding of functions Flashcards
Global variable
Defined outside the scope of a function
can be accessed from within a function
provided the function does not have a local variable with the same name
updates to global variables from within a function require the use of global keyword
Local variable
variables defined inside the scope of the function, the function body
can not be referenced outside the function
hide the global variable with the same name
passing by reference
Basic types can be reassigned within a function
Changes made to string variables inside a function are NOT reflected outside a function
Changes made to numeric variables inside a function are NOT reflected outside a function
does not affect the variables outside the function
Complex data types such as lists, dictionaries are passed by reference
reassigning complex types within a function does not affect the origional data outside the function
updating the contetnts of a complex type within a function will update the contents outside the function as well
Functions are first class citizens
everything in python is an object including the function
functions once defined, can be assigned to variables
variables can be used to invoke the function
functions can be passed in as input arguments to another function
function can be return value from a function i.e, one function can return another
Functions can be elements in a list, or tuple or dictionary
Every function name in your program should be unique
functions cannot have any name
Lambdas
python functions that have no name
Defining a function on the fly
Use and throw away functions which may not be reused
lambda>input arguments>:<expression></expression>
the body of lambda can only contain expression
The result of expression is the return value of the lambda
lamdas can be assigned to variables and invoked
Lambdas can be defined and invoked in a single statement
lambdas can accept any number of arguments can have default as well as variable length arguments
Lambdas are often used with built in filter() function in python
Part of Anaconda distribution of python
modules available are datetime, random, os
python module
contain code for variables, functions and objects