Python function Detailed Summary

Source: Internet
Author: User
Tags function definition

function definition
    1. The function code block begins with a def keyword followed by the function identifier name and parentheses ().
    2. Any incoming parameters and arguments must be placed in the middle of the parentheses. Parentheses can be used to define parameters.
    3. The first line of the function statement can optionally use the document string-for storing the function description.
    4. The function contents begin with a colon and are indented.
    5. return [expression] ends the function, optionally returning a value to the caller. Return without an expression is equivalent to returning None.
Grammar
def functionname( parameters ):   "函数_文档字符串"   function_suite   return [expression]
Can change (mutable) and non-changing (immutable) objects

In Python, ==strings, tuples, and numbers== are objects that cannot be changed, and ==list,dict== are objects that can be modified.

    • Immutable Types :

      Variable assignment a=5 and then assign value a=10, here is actually reborn into an int value object 10, and then let a point to it, and 5 is discarded, not change the value of a, the equivalent of a new student into a.

    • Variable type :

      Variable assignment la=[1,2,3,4] After assigning a value la[2]=5 is to change the value of the third element of List LA, itself LA is not moving, but its internal part of the value has been modified.

Global variables and local variables

A variable that is defined inside a function has a local scope

Defines an owning global scope outside the function.

If you want to declare a variable inside a function as an external global scope, use the global declaration

Pass the reference
    • Required Parameters
#可写函数说明def printme( str ):   "打印任何传入的字符串"   print str;   return;prinme(‘My string‘)
    • Keyword parameters
#可写函数说明def printme( str ):   "打印任何传入的字符串"   print str;   return;#调用printme函数printme( str = "My string");
    • Default parameters
#可写函数说明def printinfo( name, age = 35 ):   "打印任何传入的字符串"   print "Name: ", name;   print "Age ", age;   return;#调用printinfo函数printinfo( age=50, name="miki" );printinfo( name="miki" );
    • Indefinite length parameter

      When you pass in the list, add an asterisk
      When you pass in a dictionary, add two asterisks

# 可写函数说明def printinfo( arg1, *vartuple ):   "打印任何传入的参数"   print "输出: "   print arg1   for var in vartuple:      print var   return;# 调用printinfo 函数printinfo( 10 );printinfo( 70, 60, 50 );
Anonymous parameter lambda
    • Lambda is just an expression, and the function body is much simpler than def.
    • The body of a lambda is an expression, not a block of code. Only a finite amount of logic can be encapsulated in a lambda expression.
      The lambda function has its own namespace and cannot access parameters outside its own argument list or in the global namespace.
    • Although the lambda function appears to be only a single line, it is not equivalent to a C or C + + inline function, which is designed to call small functions without consuming stack memory to increase operational efficiency.
语法lambda [arg1 [,arg2,.....argn]]:expression# 可写函数说明sum = lambda arg1, arg2: arg1 + arg2;# 调用sum函数print "相加后的值为 : ", sum( 10, 20 )

Python function Detailed Summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.