Python中的函数Function(Part2)

本文详细介绍了Python函数中的几种特殊参数类型及其用法,包括默认参数、关键字参数和VarArgs参数等,通过具体示例帮助读者更好地理解这些参数的使用场景。

因为参数是函数中非常非常重要的一部分,而Python里面对参数的特性有很多特殊的要求,所以我们要对函数的参数有一个详细的了解。

下面我们将详细介绍几类特殊参数的用法:

默认参数

关键字参数

VarArgs参数(可变参数类型):在Python中,很多时候我们无法确定一个函数的参数个数。以一个能实现求和的函数为例,事先你不知道有多少个数会被相加,你甚至不会知道他们的类型是什么。

例:

默认参数:

# 默认参数的函数,在Python中调用函数时不需要指明所有的参数的值,如果参数没有被指明将直接使用默认值
def repeat_str(s, times = 1):
    repeated_strs = s * times
    return repeated_strs

repeated_strings = repeat_str("Happy Birthday!")      #只传入函数中第一个参数的值(字符串),并没有指明第二个参数值(重复多少次),Python将使用定义函数时参数的默认值
print(repeated_strings)
repeated_strings_2 = repeat_str("Happy Birthday!" , 4)      #当函数中两个参数都被指明时,Python会自动忽略默认值直接使用指定值
print(repeated_strings_2)

注:当我们只指定一个参数时,Python编辑器她怎么知道指定的是哪一个参数的值?Python在调用函数时,他将会在调用的函数和定义的函数里面同时取第一个参数作为对应,也就是说他会把调用函数的第一个参数值赋值给定义函数的第一个参数值,然后再依次往下对比。所以当我们在调用函数里只用一个参数时,Python首先找到定义函数的第一个参数,定义函数的其他参数将使用默认值。因此Python是按从左到右的顺序一一来找对应的,因此Python有一个限制要求,不能再默认参数后面跟随没有默认参数。

例:
#不能在有默认值的参数后面跟随没有默认值的参数
#f(a, b =2)合法
#f(a = 2, b)非法,因为Python无法进行对应

关键字参数:
例:
#关键字参数: 当我们调用函数时,我们并不想给所有的参数都传值,我们只想选择性的传入部分参数,那么我们就可以用到关键字参数
def func(a, b = 4, c = 8):      #定义函数是b、c使用默认参数
    print('a is', a, 'and b is', b, 'and c is', c)

func(13, 17)    #13与a对应,17与b对应
func(125, c = 24)   #125与a对应,跳过b的值,24与c的值对应(利用关键字参数传入)
func(c = 40, a = 80)    #在使用关键字参数传入参数时,顺序就没那么重要了
VarArgs参数:
假设在定义一个函数时,我们也不知道在调用的时候会传入多少个参数,可能会传入1个参数,可能会传入5个参数,也可能是10个参数,这时候就要用到VarArgs参数,VarArgs参数不必指明多少个参数,而是将参数的数量当做一个变量。
例:
#VarArgs参数
def print_paras(fpara, *nums, **words): #“*+参数名”Python将默认传入的参数是一个list,“**+参数名”Python将多个有关键字指明的参数与之对应
    print("fpara: " + str(fpara))
    print("nums: " + str(nums))
    print("words: " + str(words))
    
print_paras("hello", 1, 3, 5, 7, word = "python", anohter_word = "java")



### Python Join Function Usage and Examples In Python, the `str.join()` method is a string operation that returns a string in which the elements of sequence have been joined by a specified separator. The syntax for this function is as follows: ```python 'connector'.join(iterable) ``` The parameter `iterable` must be a series of strings; otherwise, a `TypeError` will occur unless all items can be converted into strings implicitly. For instance, joining words with spaces or creating comma-separated values are common use cases: ```python words = ["hello", "world"] print(' '.join(words)) # hello world csv_elements = ['apple', 'banana', 'cherry'] print(','.join(csv_elements)) # apple,banana,cherry ``` When working with file paths where slashes need to separate directory names, one might also utilize join: ```python path_parts = ['folder1', 'subfolder2', 'file.txt'] print('/'.join(path_parts)) # folder1/subfolder2/file.txt ``` To concatenate multiple lines within a single string while ensuring each part appears on its own line, newline characters serve well as separators: ```python lines = ["First line", "Second line", "Third line"] multi_line_string = '\n'.join(lines) print(multi_line_string) # First line # Second line # Third line ``` It's important to note that only string types should reside inside the iterable passed to `.join()`. If integers exist among these elements, they require conversion first using methods like list comprehension combined with str(): ```python numbers = [1, 2, 3] string_numbers = '-'.join([str(num) for num in numbers]) print(string_numbers) # 1-2-3 ``` --related questions-- 1. How does the performance of `str.join()` compare against other concatenation techniques? 2. What exceptions may arise when improperly utilizing `str.join()`? 3. Can you provide scenarios beyond those mentioned here where `str.join()` proves particularly useful? 4. Is there any difference between `''.join(list)` versus directly adding strings together via `+` operator concerning memory allocation?
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值