Python Note2 (Functions)

Labels: Python, Function

Ref:
Python Functions (https://www.tutorialspoint.com/python/python_functions.htm)
Python Modules (https://www.tutorialspoint.com/python/python_modules.htm)


Functions

  • Syntax
def functionname( parameters ):
   "function_docstring"
   function_suite
   return [expression]
  • Example
def printme( str ):
   "This prints a passed string into this function"
   print str
   return
  • All parameters in the Python language are passed by reference.
  • Maintain reference of the passed object and append values in the same object.
def changeme( mylist ):
   "This changes a passed list into this function"
   mylist.append([1,2,3,4]);
   print "Values inside the function: ", mylist
   return
  • Reference is being overwritten inside the called function.
def changeme( mylist ):
   "This changes a passed list into this function"
   mylist = [1,2,3,4]; # This would assig new reference in mylist
   print "Values inside the function: ", mylist
   return

Function Arguments

  • Required arguments
    Required arguments are the arguments passed to a function in correct positional order, otherwise it gives a syntax error.
  • Keyword arguments
    Keyword arguments are related to the function calls. When you use keyword arguments in a function call, the caller identifies the arguments by the parameter name.
# Function definition is here
def printinfo( name, age ):
   "This prints a passed info into this function"
   print "Name: ", name
   print "Age ", age
   return;
# Now you can call printinfo function
printinfo( age=50, name="miki" )
  • Default arguments
    A default argument is an argument that assumes a default value if a value is not provided in the function call for that argument.
# Function definition is here
def printinfo( name, age = 35 ):
   "This prints a passed info into this function"
   print "Name: ", name
   print "Age ", age
   return;
# Now you can call printinfo function
printinfo( age=50, name="miki" )
printinfo( name="miki" )
  • Variable-length arguments
    Syntax:
def functionname([formal_args,] *var_args_tuple ):
   "function_docstring"
   function_suite
   return [expression]

An asterisk * is placed before the variable name that holds the values of all nonkeyword variable arguments. This tuple remains empty if no additional arguments are specified during the function call.
Example:

# Function definition is here
def printinfo( arg1, *vartuple ):
   "This prints a variable passed arguments"
   print "Output is: "
   print arg1
   for var in vartuple:
      print var
   return;
# Now you can call printinfo function
printinfo( 10 )
printinfo( 70, 60, 50 )

Anonymous Functions

You can use the lambda keyword to create small anonymous functions.
Syntax:

lambda [arg1 [,arg2,.....argn]]:expression

Example:

# Function definition is here
sum = lambda arg1, arg2: arg1 + arg2;
# Now you can call sum as a function
print "Value of total : ", sum( 10, 20 )
print "Value of total : ", sum( 20, 20 )
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容