函数的参数.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # -*- coding: UTF-8 -*-
  2. # def foo2(prot=3306): # 默认参数
  3. # print(prot)
  4. # foo2()
  5. # foo2(8080) # 位置参数
  6. # foo2(prot=90) # 关键字参数
  7. # def foo(name, age=19):
  8. # print(name)
  9. # return age, 20
  10. # r = foo('xdc')
  11. # print(r)
  12. # def myfunc(x,y,z):
  13. # print(x,y,z)
  14. # tuple_vec = (1,0,10)
  15. # dict_vec = {'x':1, 'y':0, 'z':1 }
  16. # # myfunc(1,2,3) # 实参中的形参
  17. # # myfunc(z=3,x=1,y=2) #关键字参数传
  18. # myfunc(*tuple_vec) # 解包传
  19. # myfunc(**dict_vec) # 解包传 (值必须对应)
  20. # def foo(name, age, *args): # arge 是动态参数(必须是位置参数)
  21. # # 动态参数就是传入的参数的个数是动态的,可以是1个、2个到任意个,还可以是0个
  22. # print(name)
  23. # print(args)
  24. # foo('xdc',18)
  25. # foo('xdc', 18, 'a','b',[1,2,3])
  26. # def func(**kwargs): # 可以是0个或多个, 字典类型 (关键值参数)
  27. # # 而**kwargs则是将一个可变的关键字参数的字典传给函数实参
  28. # print(kwargs)
  29. # func(a=1,b=2,ip='192.168.10.102')
  30. ###============ split ============###
  31. astr = "hello world ,hhhh world heihei"
  32. def myreplace (astr,oldstr,newstr):
  33. result=astr.split(oldstr)
  34. return newstr.join(result)
  35. print(myreplace(astr,"world","job"))