zoukankan      html  css  js  c++  java
  • Python--面向过程编程

    面向过程编程是Python两种编程流派的其中一种,另外一种是面向对象编程,这篇博客只讨论面向过程编程:

    1、概念

    面向过程的核心是过程二字,过程就是解决问题的步骤,他就像是设计工厂的一条流水线,是一种机械式的思维方式

    2、优点

    复杂的问题流程化,简单化

    3、编程实例

    用户注册:

     1 import json
     2 
     3 
     4 def interactive():
     5     name = input('>>: ').strip()
     6     pwd = input('>>: ').strip()
     7     return {
     8         'name': name,
     9         'pwd': pwd
    10     }
    11 
    12 
    13 def check(user_info):
    14     is_valid = True
    15 
    16     if len(user_info['name']) == 0:
    17         print('用户名不能为空')
    18         is_valid = False
    19 
    20     if len(user_info['pwd']) < 6:
    21         print('密码不能少于6位')
    22         is_valid = False
    23 
    24     return {
    25         'is_valid': is_valid,
    26         'user_info': user_info
    27     }
    28 
    29 
    30 def register(check_info):
    31     if check_info['is_valid']:
    32         with open('db.json', 'w', encoding='utf-8') as f:
    33             json.dump(check_info['user_info'], f)
    34 
    35 
    36 def main():
    37     user_info = interactive()
    38     check_info = check(user_info)
    39     register(check_info)
    40 
    41 
    42 if __name__ == '__main__':
    43     main()

    过程分解,编程简单,但是后期更改复杂,比如增加一个填写email的选项,代码如下:

     1 import json
     2 import re
     3 
     4 
     5 def interactive():
     6     name = input('>>: ').strip()
     7     pwd = input('>>: ').strip()
     8     email = input('>>: ').strip()
     9     return {
    10         'name': name,
    11         'pwd': pwd,
    12         'email': email
    13     }
    14 
    15 
    16 def check(user_info):
    17     is_valid = True
    18 
    19     if len(user_info['name']) == 0:
    20         print('用户名不能为空')
    21         is_valid = False
    22 
    23     if len(user_info['pwd']) < 6:
    24         print('密码不能少于6位')
    25         is_valid = False
    26 
    27     if not re.search(r'@.*.com$', user_info['email']):
    28         print('邮箱格式不合法')
    29         is_valid = False
    30 
    31     return {
    32         'is_valid': is_valid,
    33         'user_info': user_info
    34     }
    35 
    36 
    37 def register(check_info):
    38     if check_info['is_valid']:
    39         with open('db.json', 'w', encoding='utf-8') as f:
    40             json.dump(check_info['user_info'], f)
    41 
    42 
    43 def main():
    44     user_info = interactive()
    45     check_info = check(user_info)
    46     register(check_info)
    47 
    48 
    49 if __name__ == '__main__':
    50     main()

    从以上代码可以看出,面向过程编程扩展性较差,有一种牵一发而动全身的影响,

    使用场景:适用于对扩展性要求不是很高的项目

  • 相关阅读:
    通过避免下列 10 个常见 ASP.NET 缺陷使网站平稳运行 from MSDN
    编写自己的dojo扩展zt
    Adding an IE7 Browser Template for use by Web Tests
    MAC地址与IP地址绑定策略的破解zt
    .net 中string 的应用特点(转贴)让我豁然开朗
    全国最佳医院排名(供参考)
    小心你的Page_Load重复执行(转贴)
    A780知识总汇zt
    [Quoted] Writing HighPerformance Managed Applications : A Primer
    [网络摘录学习]常用的Linux系统监控命令
  • 原文地址:https://www.cnblogs.com/xudachen/p/8566386.html
Copyright © 2011-2022 走看看