扫码关注官方订阅号
光阴似箭催人老,日月如移越少年。
没有恶意,但我真的怀疑楼上两位并不太了解Python。
你这么调用在Python中是没问题的,但出现这个问题的真正原因是字符串的replace方法不是用Python实现的,而是用C语言实现的,所以它不支持Python里面的keyword参数特性。
replace
keyword
你可以试一下用Python版本的replace:
Python版本的replace
from string import replace s = '012-3456' print replace(s, new='', old='-') # 即使将old和new调换位置一样可以正确替换,输出0123456
这个replace方法在string模块中(Lib/string.py文件),是对C语言版本的relace方法的封装,有兴趣的话你可以去看看它的源码
string
relace
replace函数在python2.7的文档中描述如下:
str.replace(old, new[, count]) Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.
在python中的函数参数分为四种:必选参数、默认参数、可变参数、关键字参数replace函数中old和new输入必选参数,count属于默认参数你在test1中使用的调用方式必须在函数定义时声明为关键字参数
关键字参数举例:
def test(**kw): for key in kw: print "[%s, %s]" % (key, kw[key]) test(x=9)
以上代码输出为[x, 9]
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
没有恶意,但我真的怀疑楼上两位并不太了解Python。
你这么调用在Python中是没问题的,但出现这个问题的真正原因是字符串的
replace方法不是用Python实现的,而是用C语言实现的,所以它不支持Python里面的keyword参数特性。你可以试一下用
Python版本的replace:这个
replace方法在string模块中(Lib/string.py文件),是对C语言版本的relace方法的封装,有兴趣的话你可以去看看它的源码replace函数在python2.7的文档中描述如下:
在python中的函数参数分为四种:必选参数、默认参数、可变参数、关键字参数
replace函数中old和new输入必选参数,count属于默认参数
你在test1中使用的调用方式必须在函数定义时声明为关键字参数
关键字参数举例:
以上代码输出为[x, 9]