程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python -- function

編輯:Python

Defined function

We first define a simple function , Print a greeting statement .

def greet_user():
""" Show simple greetings """
print('hello')
greet_user()

Code using def tell Python You need to define a function , Write the function name , The definition ends with a colon , Such as :def greet_user():

After definition , All the following indents form the body of the function , In the body of the function , We write out the functions of the functions we need . When you call a function , Just enter the function .

hello

Passing information to a function

In function definition def greet_user() Add in brackets username, You can make the function accept what you give username Any value , When you call a function , Pass the input information to it .

def greet_user(username):
""" Show simple greetings """
print('hello ' + username.title())
greet_user('jake')

Code greet_user('jake') Call function greet_user(), And provide it with execution print The information required by the statement .

hello Jake

Arguments and formal arguments  

In function greet_user() The definition of , Variable username It's just a parameter , In the code greet_user('jake') in ,Jake It's just an argument . Arguments are information passed to a function when it is called .

Transfer argument

When calling a function, each argument in the function call must be associated with each formal parameter in the function definition , The simplest association is based on the order of arguments , This association is called positional arguments . as follows :

def describe_pet(animal_type, pet_name):
""" Show pet information """
print("\nI have a " + animal_type + ".")
print("My " + animal_type + "'s name is" + pet_name.title() + '.')
describe_pet('hamster','harry')

Call function describe_pet() when , We provide two arguments in order, which are stored in the arguments , We output information about two pets :

I have a hamster.
My hamster's name is Harry.

We can call the function as many times as we want , Just call the function again . as follows :

def describe_pet(animal_type, pet_name):
""" Show pet information """
print("\nI have a " + animal_type + ".")
print("My " + animal_type + "'s name is" + pet_name.title() + '.')
describe_pet('hamster','harry')
describe_pet('dog','willie')

Also provide arguments to functions , Associate an argument to a formal parameter , Output results :

I have a hamster.
My hamster's name is Harry.
I have a dog.
My dog's name is Willie.

Calling a function multiple times is a highly efficient way to work , In the function , Any number of positional arguments can be used as needed ,Python The arguments called in the function will be sequentially associated with the corresponding formal parameters in the function definition .

The order of positional arguments is important . When calling a function with a positional argument , If the order of arguments is not correct , The result is bound to be inconsistent with the expectation .

def describe_pet(animal_type, pet_name):
""" Show pet information """
print("\nI have a " + animal_type + ".")
print("My " + animal_type + "'s name is" + pet_name.title() + '.')
describe_pet('willie','dog')

Above , We will input the pet's name and type upside down , The parameters passed to the associated formal parameters are automatically matched according to the order in which we input the formal parameters , It will output in the order we input .

I have a willie.
My willie's name is Dog.

In the calling function , Let's specify the name first , Then specify the type , Because of the actual parameters willie before , This value will be stored in the formal parameter animal_type in ; Again , Actual parameters dog Store in formal parameter pet_name in , The output is the exact opposite of what we want .

To solve this problem , We can use keyword arguments , Directly associate the name with the value in the argument .

def describe_pet(animal_type, pet_name):
""" Show pet information """
print("\nI have a " + animal_type + ".")
print("My " + animal_type + "'s name is" + pet_name.title() + '.')
describe_pet(animal_type='dog',pet_name=willie)
describe_pet(pet_name='willie',animal='dog')

Keyword arguments let us ignore the order of arguments in a function call , It also points out the purpose of each value in the function call .

I have a dog.
My dog's name is Willie.
I have a dog.
My dog's name is Willie.

When using keyword arguments , The formal parameter name in the function definition must be accurately indicated .

The default value is : When you write a function , You can assign values to each parameter , When an argument is provided to a formal parameter in the calling function ,Python The specified argument value will be used , otherwise , The default value of the formal parameter will be used . For example, we like formal parameters animal_type Provide default values dog, In unspecified animal_type Parameter of , It will automatically use the default values .

def describe_pet(animal_type='dog', pet_name):
""" Show pet information """
print("\nI have a " + animal_type + ".")
print("My " + animal_type + "'s name is" + pet_name.title() + '.')
describe_pet(pet_name=willie)

If we call a function , It is found that the values of certain types of arguments we need to provide are mostly the same , We can set the associated formal parameter as the default value , Reduce the input of arguments when calling functions , Make input easier .

I have a dog.
My dog's name is Willie.

Of course , When we call a function , When the supplied argument is different from the default argument value of the formal parameter , We just need to input as before , When an argument is provided to a formal parameter in the called function , The specified argument value will be used , It's no longer the default .

def describe_pet(animal_type='dog', pet_name):
""" Show pet information """
print("\nI have a " + animal_type + ".")
print("My " + animal_type + "'s name is" + pet_name.title() + '.')
describe_pet(animal_type='cat',pet_name=willie)
I have a cat.
My dog's name is Willie.

Due to the display of animal_type Provides an argument value , therefore Python The default value of this parameter will be ignored .

Avoid argument errors , After using function , If you encounter an argument mismatch error , That is, we provide more or less arguments than the function needs to do its job , At this time, we go back to check the block we call on the function , Just provide the correct number of arguments .

Return value

 


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved