Python Learning Summary 18: function Parameter Chapter

Source: Internet
Author: User

1. Determine if a function can be called

Import math>>> x = 1>>> y = math.sqrt>>> callable (x) False>>> callable (y) True

Note that the function callable is no longer available in Python 3.0 and needs to be replaced with an expression hasattr (func, __call).

2. function (or Class) interpretation

1) function comment, beginning with "#" comment

2) The document string, which is stored as part of the function if the string is written at the beginning of the function, which is called the document string.

def Square (x):     ' calculates the square of the number X '    return x*x>>> square.__ doc__'calculates the square of the numberX  '>>> in module main:square (x)    calculates the square of the Number x.

3. Method of transfer of function parameters

1) passed by function parameter order

def Hello (greeting, name):     return " %s,%s "% (greeting, name)
>>> Hello ('hello'' World') Hello, world 

2) Use keywords and default values

 def  hello_1 (greeting =  " hello  , name = "  world    ):  print   %s,%s   ' %
def '  World ' ):     print'%s,%s'%(greeting, name)>>>  Hello_2 ('hi') Hi, World

3) variable number of parameters

def print_params (*params):    print  params>>>print_ params ('  testing') ('testing',)>>> Print_params (1, 2, 3) (1, 2, 3)

As can be seen from the above example, a tuple is printed. If used in conjunction with common parameters

def Print_ params_2 (title, *params):    print  title    Print  params >>> print_params_2 (' params: ' 1, 2, 3) params: (1, 2, 3)>>> Print_ Params_2 (' Nothing: ') Nothing: ()

But you can't handle keywords.

>>>print_params_ 2 ('Hmm ... ', something=42)Traceback (most recent call last):   "<pyshell#60>"in?     2 ('Hmm ... ', something=42)'something'

4) variable number of parameters, and can handle the keyword

def print_ Params_3 (* *params)    :print  params>>> print_params_ 3 (x=1 , y=2, z=3) {'z'x'y ': 2}

A dictionary is returned.

The method of all parameter passing, put together to use

def Print_ params_4 (x, Y, z=3, *pospar, * *keypar)    :print  x, y,    zprint< /c6> pospar    print  keypar>>> print_params Less (1, 2, 3,  5, 6, 7, foo=l, bar=2 )1 2 3(5, 6, 7) {foo:'bar': 2}>> > Print_params_4 (1, 2)1 2 3() {}

Python Learning Summary 18: function Parameter Chapter

Related Article

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.