Python tutorials on how to set the default parameters of the function

Today Marco education articles to share with everyone Python tutorial is how to set the default parameters of the function? We ended up speaking on a saved game file with the contents of Python, that we talk to supplement a small tip on setting the default function parameter Oh! Getting Started with Python newbie and are P ython learning little friends come look at it, I hope this can help you!

Before we used functions, such as:

def hello(name):

print 'hello ' + name

Then we went to call this function:

hello('world')

The program will output

hello world

Many times if we are to call this function with the world, a small number of cases will go and change parameters. So, we can give the function a default parameter:

def hello(name = 'world'):

print 'hello ' + name

When you no value, this parameter will use the default values; if you provide them with your give.

Thus, by default, you can just call

hello()

You can output

hello world

Similarly, you can also specify parameters:

hello('python')

Export

hello python

Note that when there is a function of several parameters, if you want to give some parameters provide default parameters, these parameters must be at the end of the parameter. such as:

def func(a, b=5)

is correct

def func(a=5, b)

Error occurs

I congratulate you on the road and adhere to the Python's day, quickly try to look at your code has not been able to set up an alternative, some may wish to change it, make a little elegant Python programming a little bit Oh!

These are the Marco education today is to share articles on how to set up Python tutorial function default parameters, I hope this article can help small partners are engaged in learning and python python related work, you want to learn more remember concern Marco knowledge Education official website. Finally, I wish the small partners work smoothly!

Guess you like

Origin www.cnblogs.com/woshijiuke/p/12123441.html