Custom functions in Python learning notes _python

Source: Internet
Author: User

Define a function that doesn't do anything

Copy Code code as follows:

>>> def A ():
. Pass
...

>>> def Printhello ():
... print ("Hello")
...
>>> Printhello ()
Hello
>>> Callable (Printhello)
True


As the name suggests, the callable function is used to determine whether a function can be invoked;

There are books saying that callable is no longer used in Python3.0, instead of using Hasattr (func, ' __call__ ');

Copy Code code as follows:

>>> hasattr (Printhello, ' __call__ ')
True

>>> printhello.__doc__
>>> def Printhello ():
... ' Just print hello '
... print (' Hello ')
...
>>> printhello.__doc__
' Just print hello '

Each function has a __doc__ attribute, and a double underline indicates that it is a special property;

The built-in help function is useful to provide helpful information about methods/functions;
Copy Code code as follows:

>>> Help (Printhello)

The annotation information of the function is contained therein;

Although the Printhello function does not use return, you can receive the returned value with a variable:

Copy Code code as follows:

>>> result = Printhello ()
Hello
>>> result
>>> Print (Result)
None

None is Python's built-in value, a JavaScript-like undefined?

Defines a printstr that can receive parameters to print a string
Copy Code code as follows:

>>> def printstr (str):
... print (str)
...

>>> printstr ("Hello")
Hello

Like C + +, Python supports default parameters
Copy Code code as follows:

>>> def printstr (str= "Nothing"):
... print (str)
..

>>> Printstr ()
Nothing

Let's take a look at the mode of communication.
Copy Code code as follows:

>>> a = [1,2]
>>> def try_change_list (a):
... a[:] = [1,1,1]
...
>>> Try_change_list (a)
>>> A
[1, 1, 1]

Python's arguments can be understood to be passed by value (same java,javascript)?

BTW: If you don't want Try_change_list to change the original object, you can pass in the a[:]
Copy Code code as follows:

>>> a = [1,2]
>>> try_change_list (a[:])
>>> A
[1, 2]

Of course, this is a shallow copy, you can use the Copy module deepcopy to make a deep copy;

In addition to supporting parameter defaults, named passes are supported:
Copy Code code as follows:

>>> def sum (a=0, b=0):
... return a + B;
...
>>> sum (2,2)
4
>>> sum (b = 3, a = 4)
7

This kind of characteristic is more useful when the parameter is more;

Take a look at Python's support for variable parameter lists:
Copy Code code as follows:

>>> def sum (*args):
... s = 0;
... for i in args:
.. s + = i;
... return s
...
>>> sum (1,2,3,4)
10

This is a simple summation example that Python does not limit the type of arguments passed in to the SUM function, unlike the static type of C + +:
Copy Code code as follows:

>>> def printars (*args):
... for a in args:
... print (a)
...
>>> Printars (2, 3, [2,2], (2,), ' DF ')
2
3
[2, 2]
(2,)
Df
>>> Printars (* (2, 3, [2,2], (2,), ' DF ')
2
3
[2, 2]
(2,)
Df
>>> Printars (*[2, 3, [2,2], (2,), ' DF '])
2
3
[2, 2]
(2,)
Df

Here the args corresponds to the arguments of JavaScript;

In addition to receiving variable parameters using a tuple (tuple), you can also use dictionary to receive named parameters:
Copy Code code as follows:

>>> def printars (**args):
... for k in args:
... print (repr (k) + "=" + repr (Args[k]))
...
>>>
>>> printars (name= ' Wyj ', age=24)
' Name ' = ' Wyj '
' Age ' = 24
>>> printars (**{' name ': ' Wyj ', ' Age ': 24})
' Name ' = ' Wyj '
' Age ' = 24

Of course, more complex, you can mix *arg, **arg, default value attributes:

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.