py之路——day11-20230518:函数与函数式编程
作者:zb
一、函数与函数式编程
1、编程的方式 编程方式目前为止总共有三种范式:
⑴面向对象编程,核心是“类”,在py中定义类的关键字是“class”
⑵面向过程编程,核心是“过程”,在py中定义过程的关键字是“def”
⑶函数式编程,核心是“函数”,在py中定义函数的关键字是“def”,函数式编程范式比较古老并且函数式编程不需要变量,一切参数值的传递都是通过函数进行的,比较复杂,py不是一种函数式编程的语言,它是一种面向对象的编程语言
2、过程与函数的区别
综上所得,在py中,函数和过程的定义关键字相同都是def,那么在py中过程和函数有什么区别?在py中,过程是没有返回值(由return关键字定义的值)的函数,但是py给过程隐式的返回了一个“None”,代码如下:
# -*- coding:utf-8 -*- # 作者:朱博 # 函数 def func1(): """ testing1 :return: """ print("in the func1") return 0 # 过程 def func2(): """ testing2 :return: """ print("in the func2") x = func1() y = func2() print("from func1 x is %s", x) print("from func2 y is %s", y)
代码执行结果:
1 D:\oldboy_py\venv\Scripts\python.exe D:/oldboy_py/day1-20230512函数与函数式编程/过程与函数的区别.py 2 in the func1 3 in the func2 4 from func1 x is %s 0 5 from func2 y is %s None
3、为什么要使用函数?
⑴代码的复用性:
编程过程中如果遇到可以重复使用的逻辑一定要使用函数,方便重复利用
⑵保持一致性:
如果要修改一段复用代码的逻辑,可以通过修改单个函数的方式达到同时修改程序中所有引用该函数的地方的目的
⑶可扩展性:
可以随时修改函数的逻辑来满足程序的需要
举例代码如下:
1 # 打印日志功能 2 import time 3 4 5 def logger(): 6 """ 7 实现打印日志的功能 8 :return: 9 """ 10 time_format = "%Y-%m-%d %X" 11 time_current = time.strftime(time_format) 12 with open("a.txt", "a+") as f: 13 f.write("%s end action\n" % time_current) 14 15 16 def test1(): 17 """test1""" 18 print("in the test1") 19 logger() 20 21 22 def test2(): 23 """test2""" 24 print("in the test2") 25 logger() 26 27 28 def test3(): 29 """test3""" 30 print("in the test3") 31 logger() 32 33 34 test1() 35 test2() 36 test3()
代码执行结果:
1 D:\oldboy_py\venv\Scripts\python.exe D:/oldboy_py/day1-20230512函数与函数式编程/为什么要使用函数.py 2 in the test1 3 in the test2 4 in the test3 5 6 Process finished with exit code 0
生成文件内容:
1 2023-05-18 21:47:20 end action
2 2023-05-18 21:47:20 end action
3 2023-05-18 21:47:20 end action
二、函数式编程之参数详解
1、函数的返回值
⑴函数在运行到return语句后就不再继续向下执行了,无论后面还有什么语句都不再执行
示例代码如下:
1 def test1(): 2 """ 3 test1 4 :return: 5 """ 6 print("in the test1") 7 return 0 8 print("test end") 9 10 11 test1()
执行结果:
1 D:\oldboy_py\venv\Scripts\python.exe D:/oldboy_py/day1-20230512函数与函数式编程/函数运行碰到return关键字就结束运行了.py
2 in the test1
3
4
Process finished with exit code 0
⑵函数的返回值类型:
a、函数的返回值个数=0,则函数返回None
b、函数的返回值个数=1,则函数返回该值
c、函数的返回值个数>1,则函数返回一个将多个值包含起来的元组
示例代码如下:
1 def test1(): 2 """ 3 test1返回值个数=0 4 :return: 5 """ 6 print("in the test1") 7 8 9 def test2(): 10 """ 11 test2返回值个数=1 12 :return: 13 """ 14 print("in the test2") 15 return 0 16 17 18 def test3(): 19 """ 20 test3返回值个数>1 21 :return: 22 """ 23 print("in the test3") 24 return 1, "a", ['zb', 'aaa'] 25 26 27 x = test1() 28 y = test2() 29 z = test3() 30 print(x, "\n", y, "\n", z)
执行结果:
1 D:\oldboy_py\venv\Scripts\python.exe D:/oldboy_py/day1-20230512函数与函数式编程/函数的返回值个数.py 2 in the test1 3 in the test2 4 in the test3 5 None 6 0 7 (1, 'a', ['zb', 'aaa'])
⑶函数可以返回一个已经被定义的函数的的返回值,也可以返回该函数的内存地址(不加括号返回即可),这就是所谓的高阶函数
示例代码如下:
1 def test1(): 2 """ 3 test1 4 :return: 5 """ 6 return "in the test1" 7 8 # 返回内存地址 9 def test2(): 10 """ 11 test2 12 :return: 13 """ 14 return test1 15 16 # 返回函数值 17 def test3(): 18 """ 19 test3 20 :return: 21 """ 22 return test1() 23 24 x = test2() 25 y = test3() 26 print(x) 27 print(y)
执行结果:
1 D:\oldboy_py\venv\Scripts\python.exe D:/oldboy_py/day1-20230512函数与函数式编程/函数可以返回已经被定义的函数的返回值.py 2 <function test1 at 0x0000024A0CE8E9E0> 3 in the test1 4 5 Process finished with exit code 0
⑷函数的实参和形参
a、实参
实际占用内存空间的参数
b、形参
只是对实参的引用
c、位置参数
实参和形参必须一一对应传递的参数
d、关键字参数
按照函数定义的形参去传递参数,与定义形参时的顺序无关
e、位置参数+关键字参数混合调用
位置参数和关键字参数混合调用的时候,关键字参数必须在位置参数之后
示例代码如下:
1 def test(x, y, z): 2 """ 3 test 4 :param x: 5 :param y: 6 :param z: 7 :return: 8 """ 9 print(x) 10 print(y) 11 print(z) 12 13 14 # 位置调用 15 test(1, 2, 3) 16 # 关键字调用 17 test(y=2, x=1, z=3) 18 # 位置+关键字混合调用 19 test(1, 2, z=3)
执行结果:
1 D:\oldboy_py\venv\Scripts\python.exe D:/oldboy_py/day1-20230512函数与函数式编程/函数的参数.py 2 1 3 2 4 3 5 1 6 2 7 3 8 1 9 2 10 3 11 12 Process finished with exit code 0
f、函数的默认参数
->默认参数的特点:
在函数定义的时候,默认就给形参赋值,如果在函数调用的时候没有给默认参数传值,那么就使用定义时的默认值,因此默认参数也叫非必传参数
->默认参数的用途:
例如在软件安装的时候默认的配置等
示例代码如下:
1 def test(x, y=2): 2 """ 3 test 4 :param x: 5 :param y: 6 :return: 7 """ 8 print(x) 9 print(y) 10 11 12 test(1)
执行结果:
1 D:\oldboy_py\venv\Scripts\python.exe D:/oldboy_py/day1-20230512函数与函数式编程/函数的默认参数.py 2 1 3 2 4 5 Process finished with exit code 0
g、函数的参数组
->*args:
在定义形参时,*args表示该形参可以接收个数不固定的位置参数并将他们放在一个元组中,保存在args里
->**kwargs:
在定义形参时,**kwargs表示该形参可以接收个数不固定的关键字参数并将他们放在一个字典中,保存在kwargs里
->在定义函数时,参数组一定要放在形参的最后
以下代码举例说明位置参数+默认参数+关键字参数+参数组混合调用的参数值传递:
1 def test4(name, age=18, *args, **kwagrs): 2 """ 3 4 :param name: 5 :param age: 6 :param args: 7 :param kwagrs: 8 :return: 9 """ 10 print(name) 11 print(age) 12 print(args) 13 print(kwagrs) 14 15 16 test4("朱博", 34, sex="男", hobby='tesla')
执行结果:
1 D:\oldboy_py\venv\Scripts\python.exe D:/oldboy_py/day1-20230512函数与函数式编程/参数组.py 2 朱博 3 34 4 () 5 {'sex': '男', 'hobby': 'tesla'} 6 7 Process finished with exit code 0
h、函数的局部变量和全局变量以及作用域
->局部变量:
只在函数体内生效的变量就是局部变量,不会影响函数体外同名变量值的变量,但是在py中列表、集合、字典以及类等变量除外
->全局变量:
在整个程序中均生效的变量,如果要在单个函数体内修改全局变量的值的话需要在函数体内首先使用global关键字声明全局变量(只有字符串和整数需要先声明,列表、集合、字典以及类等可以不用声明直接在函数体内修改),注意:尽量不要在函数体内修改全局变量的值
示例代码如下:
1 name: str = "朱博" 2 school = "西安邮电大学" 3 names = ['朱博', '白木优子', '谷原希美'] 4 5 6 def change_name(name): 7 """ 8 change_name 9 :param name: 10 :return: 11 """ 12 # global name 13 global school # 全局变量 14 school = "西北农林科技大学" # 修改了全局变量的值 15 print("before change name is %s" % name) 16 name = "zb" # 局部变量 17 print("after change name is %s" % name) 18 names[0] = 'zb' 19 print('inner names %s' % names) 20 21 22 change_name(name) 23 print(name) 24 print(school) 25 print('outer name %s' % names)
执行结果:
1 D:\oldboy_py\venv\Scripts\python.exe D:/oldboy_py/day1-20230512函数与函数式编程/局部变量与全局变量的作用域.py 2 before change name is 朱博 3 after change name is zb 4 inner names ['zb', '白木优子', '谷原希美'] 5 朱博 6 西北农林科技大学 7 outer name ['zb', '白木优子', '谷原希美'] 8 9 Process finished with exit code 0
三、递归函数
1、递归函数的概念
如果一个函数在其内部调用了自身,那么这个函数就是递归函数
2、递归函数的特点
⑴递归函数必须要有一个明确的结束条件
这是py程序本身的保护机制,最多允许py函数递归999次就自动报错停止调用,否则程序会耗尽机器资源
示例代码1如下:
1 def cal(n): 2 """ 3 4 :param n: 5 :return: 6 """ 7 print(n) 8 return cal(n+1) 9 10 11 cal(0)
执行结果1:

1 D:\oldboy_py\venv\Scripts\python.exe D:/oldboy_py/day1-20230512函数与函数式编程/递归必须要有一个明确的结束条件.py 2 0 3 1 4 2 5 3 6 4 7 5 8 6 9 7 10 8 11 9 12 10 13 11 14 12 15 13 16 14 17 15 18 16 19 17 20 18 21 19 22 20 23 21 24 22 25 23 26 24 27 25 28 26 29 27 30 28 31 29 32 30 33 31 34 32 35 33 36 34 37 35 38 36 39 37 40 38 41 39 42 40 43 41 44 42 45 43 46 44 47 45 48 46 49 47 50 48 51 49 52 50 53 51 54 52 55 53 56 54 57 55 58 56 59 57 60 58 61 59 62 60 63 61 64 62 65 63 66 64 67 65 68 66 69 67 70 68 71 69 72 70 73 71 74 72 75 73 76 74 77 75 78 76 79 77 80 78 81 79 82 80 83 81 84 82 85 83 86 84 87 85 88 86 89 87 90 88 91 89 92 90 93 91 94 92 95 93 96 94 97 95 98 96 99 97 100 98 101 99 102 100 103 101 104 102 105 103 106 104 107 105 108 106 109 107 110 108 111 109 112 110 113 111 114 112 115 113 116 114 117 115 118 116 119 117 120 118 121 119 122 120 123 121 124 122 125 123 126 124 127 125 128 126 129 127 130 128 131 129 132 130 133 131 134 132 135 133 136 134 137 135 138 136 139 137 140 138 141 139 142 140 143 141 144 142 145 143 146 144 147 145 148 146 149 147 150 148 151 149 152 150 153 151 154 152 155 153 156 154 157 155 158 156 159 157 160 158 161 159 162 160 163 161 164 162 165 163 166 164 167 165 168 166 169 167 170 168 171 169 172 170 173 171 174 172 175 173 176 174 177 175 178 176 179 177 180 178 181 179 182 180 183 181 184 182 185 183 186 184 187 185 188 186 189 187 190 188 191 189 192 190 193 191 194 192 195 193 196 194 197 195 198 196 199 197 200 198 201 199 202 200 203 201 204 202 205 203 206 204 207 205 208 206 209 207 210 208 211 209 212 210 213 211 214 212 215 213 216 214 217 215 218 216 219 217 220 218 221 219 222 220 223 221 224 222 225 223 226 224 227 225 228 226 229 227 230 228 231 229 232 230 233 231 234 232 235 233 236 234 237 235 238 236 239 237 240 238 241 239 242 240 243 241 244 242 245 243 246 244 247 245 248 246 249 247 250 248 251 249 252 250 253 251 254 252 255 253 256 254 257 255 258 256 259 257 260 258 261 259 262 260 263 261 264 262 265 263 266 264 267 265 268 266 269 267 270 268 271 269 272 270 273 271 274 272 275 273 276 274 277 275 278 276 279 277 280 278 281 279 282 280 283 281 284 282 285 283 286 284 287 285 288 286 289 287 290 288 291 289 292 290 293 291 294 292 295 293 296 294 297 295 298 296 299 297 300 298 301 299 302 300 303 301 304 302 305 303 306 304 307 305 308 306 309 307 310 308 311 309 312 310 313 311 314 312 315 313 316 314 317 315 318 316 319 317 320 318 321 319 322 320 323 321 324 322 325 323 326 324 327 325 328 326 329 327 330 328 331 329 332 330 333 331 334 332 335 333 336 334 337 335 338 336 339 337 340 338 341 339 342 340 343 341 344 342 345 343 346 344 347 345 348 346 349 347 350 348 351 349 352 350 353 351 354 352 355 353 356 354 357 355 358 356 359 357 360 358 361 359 362 360 363 361 364 362 365 363 366 364 367 365 368 366 369 367 370 368 371 369 372 370 373 371 374 372 375 373 376 374 377 375 378 376 379 377 380 378 381 379 382 380 383 381 384 382 385 383 386 384 387 385 388 386 389 387 390 388 391 389 392 390 393 391 394 392 395 393 396 394 397 395 398 396 399 397 400 398 401 399 402 400 403 401 404 402 405 403 406 404 407 405 408 406 409 407 410 408 411 409 412 410 413 411 414 412 415 413 416 414 417 415 418 416 419 417 420 418 421 419 422 420 423 421 424 422 425 423 426 424 427 425 428 426 429 427 430 428 431 429 432 430 433 431 434 432 435 433 436 434 437 435 438 436 439 437 440 438 441 439 442 440 443 441 444 442 445 443 446 444 447 445 448 446 449 447 450 448 451 449 452 450 453 451 454 452 455 453 456 454 457 455 458 456 459 457 460 458 461 459 462 460 463 461 464 462 465 463 466 464 467 465 468 466 469 467 470 468 471 469 472 470 473 471 474 472 475 473 476 474 477 475 478 476 479 477 480 478 481 479 482 480 483 481 484 482 485 483 486 484 487 485 488 486 489 487 490 488 491 489 492 490 493 491 494 492 495 493 496 494 497 495 498 496 499 497 500 498 501 499 502 500 503 501 504 502 505 503 506 504 507 505 508 506 509 507 510 508 511 509 512 510 513 511 514 512 515 513 516 514 517 515 518 516 519 517 520 518 521 519 522 520 523 521 524 522 525 523 526 524 527 525 528 526 529 527 530 528 531 529 532 530 533 531 534 532 535 533 536 534 537 535 538 536 539 537 540 538 541 539 542 540 543 541 544 542 545 543 546 544 547 545 548 546 549 547 550 548 551 549 552 550 553 551 554 552 555 553 556 554 557 555 558 556 559 557 560 558 561 559 562 560 563 561 564 562 565 563 566 564 567 565 568 566 569 567 570 568 571 569 572 570 573 571 574 572 575 573 576 574 577 575 578 576 579 577 580 578 581 579 582 580 583 581 584 582 585 583 586 584 587 585 588 586 589 587 590 588 591 589 592 590 593 591 594 592 595 593 596 594 597 595 598 596 599 597 600 598 601 599 602 600 603 601 604 602 605 603 606 604 607 605 608 606 609 607 610 608 611 609 612 610 613 611 614 612 615 613 616 614 617 615 618 616 619 617 620 618 621 619 622 620 623 621 624 622 625 623 626 624 627 625 628 626 629 627 630 628 631 629 632 630 633 631 634 632 635 633 636 634 637 635 638 636 639 637 640 638 641 639 642 640 643 641 644 642 645 643 646 644 647 645 648 646 649 647 650 648 651 649 652 650 653 651 654 652 655 653 656 654 657 655 658 656 659 657 660 658 661 659 662 660 663 661 664 662 665 663 666 664 667 665 668 666 669 667 670 668 671 669 672 670 673 671 674 672 675 673 676 674 677 675 678 676 679 677 680 678 681 679 682 680 683 681 684 682 685 683 686 684 687 685 688 686 689 687 690 688 691 689 692 690 693 691 694 692 695 693 696 694 697 695 698 696 699 697 700 698 701 699 702 700 703 701 704 702 705 703 706 704 707 705 708 706 709 707 710 708 711 709 712 710 713 711 714 712 715 713 716 714 717 715 718 716 719 717 720 718 721 719 722 720 723 721 724 722 725 723 726 724 727 725 728 726 729 727 730 728 731 729 732 730 733 731 734 732 735 733 736 734 737 735 738 736 739 737 740 738 741 739 742 740 743 741 744 742 745 743 746 744 747 745 748 746 749 747 750 748 751 749 752 750 753 751 754 752 755 753 756 754 757 755 758 756 759 757 760 758 761 759 762 760 763 761 764 762 765 763 766 764 767 765 768 766 769 767 770 768 771 769 772 770 773 771 774 772 775 773 776 774 777 775 778 776 779 777 780 778 781 779 782 780 783 781 784 782 785 783 786 784 787 785 788 786 789 787 790 788 791 789 792 790 793 791 794 792 795 793 796 794 797 795 798 796 799 797 800 798 801 799 802 800 803 801 804 802 805 803 806 804 807 805 808 806 809 807 810 808 811 809 812 810 813 811 814 812 815 813 816 814 817 815 818 816 819 817 820 818 821 819 822 820 823 821 824 822 825 823 826 824 827 825 828 826 829 827 830 828 831 829 832 830 833 831 834 832 835 833 836 834 837 835 838 836 839 837 840 838 841 839 842 840 843 841 844 842 845 843 846 844 847 845 848 846 849 847 850 848 851 849 852 850 853 851 854 852 855 853 856 854 857 855 858 856 859 857 860 858 861 859 862 860 863 861 864 862 865 863 866 864 867 865 868 866 869 867 870 868 871 869 872 870 873 871 874 872 875 873 876 874 877 875 878 876 879 877 880 878 881 879 882 880 883 881 884 882 885 883 886 884 887 885 888 886 889 887 890 888 891 889 892 890 893 891 894 892 895 893 896 894 897 895 898 896 899 897 900 898 901 899 902 900 903 901 904 902 905 903 906 904 907 905 908 906 909 907 910 908 911 909 912 910 913 911 914 912 915 913 916 914 917 915 918 916 919 917 920 918 921 919 922 920 923 921 924 922 925 923 926 924 927 925 928 926 929 927 930 928 931 929 932 930 933 931 934 932 935 933 936 934 937 935 938 936 939 937 940 938 941 939 942 940 943 941 944 942 945 943 946 944 947 945 948 946 949 947 950 948 951 949 952 950 953 951 954 952 955 953 956 954 957 955 958 956 959 957 960 958 961 959 962 960 963 961 964 962 965 963 966 964 967 965 968 966 969 967 970 968 971 969 972 970 973 971 974 972 975 973 976 974 977 975 978 976 979 977 980 978 981 979 982 980 983 981 984 982 985 983 986 984 987 985 988 986 989 987 990 988 991 989 992 990 993 991 994 992 995 993 996 994 997 995 998 Traceback (most recent call last): 999 File "D:\oldboy_py\day1-20230512函数与函数式编程\递归必须要有一个明确的结束条件.py", line 15, in <module> 1000 cal(0) 1001 File "D:\oldboy_py\day1-20230512函数与函数式编程\递归必须要有一个明确的结束条件.py", line 12, in cal 1002 return cal(n+1) 1003 File "D:\oldboy_py\day1-20230512函数与函数式编程\递归必须要有一个明确的结束条件.py", line 12, in cal 1004 return cal(n+1) 1005 File "D:\oldboy_py\day1-20230512函数与函数式编程\递归必须要有一个明确的结束条件.py", line 12, in cal 1006 return cal(n+1) 1007 [Previous line repeated 993 more times] 1008 File "D:\oldboy_py\day1-20230512函数与函数式编程\递归必须要有一个明确的结束条件.py", line 11, in cal 1009 print(n) 1010 RecursionError: maximum recursion depth exceeded while calling a Python object 1011 1012 Process finished with exit code 1
示例代码2如下:
1 def cal(n): 2 """ 3 4 :param n: 5 :return: 6 """ 7 print(n) 8 if int(n/2) > 0: 9 return cal(int(n / 2)) 10 print("->", n) 11 12 13 cal(10)
执行结果2:
1 D:\oldboy_py\venv\Scripts\python.exe D:/oldboy_py/day1-20230512函数与函数式编程/递归必须要有一个明确的结束条件.py 2 10 3 5 4 2 5 1 6 -> 1 7 8 Process finished with exit code 0
⑵每次进入更深一层递归时,问题规模要比上一层减少
⑶递归效率不高,还会导致栈溢出,因此不建议使用递归
四、高阶函数
1、高阶函数的概念
如果一个函数可以接收另外一个函数作为参数,那么这个函数就是高阶函数
示例代码如下:
1 def add(a, b, f): 2 """ 3 4 :return: 5 """ 6 return f(a) + f(b) 7 8 9 res = add(3, -6, abs) 10 print(res)
执行结果:
1 D:\oldboy_py\venv\Scripts\python.exe D:/oldboy_py/day1-20230512函数与函数式编程/高阶函数.py 2 9 3 4 Process finished with exit code 0
本文来自博客园,作者:投资交易大师,转载请注明原文链接:https://www.cnblogs.com/qq2751044056/p/17413376.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?