• question


     Q:1.1、optparse参数处理

    --------------------------------------------------------------------------------------------------------------------------------------------------------

    1、带参数装饰器执行步骤

     1 #本地验证
     2 user,passwd = "abc","123"
     3  
     4 def auth(auth_type):  
     5     print("auth func:",auth_type)
     6     def outer_wrapper(func):   
     7         def wrapper(*args,**kwargs):  
     8             print("wrapper func args:",*args,**kwargs)
     9             username = input("Username:").strip()
    10             password = input("Password:").strip()
    11             if auth_type == "local":
    12                 if user == username and passwd == password:
    13                     print("33[32mUser has passed authentication33[0m")
    14                     res = func(*args,**kwargs)
    15                     print("--after authentication")
    16                     return res
    17                 else:
    18                     exit("Invalid username or password")
    19             elif auth_type == "ldap":
    20                 pass
    21         return wrapper
    22     return outer_wrapper
    23  
    24 def index():
    25     print("welcome to index page")
    26  
    27 @auth(auth_type="local")  #带参数装饰器
    28 def home():
    29     print("welcome to home page")
    30     return "from home"
    31  
    32 @auth(auth_type="ldap")   #带参数装饰器
    33 def bbs():
    34     print("welcome  to bbs page")
    35  
    36 index()
    37 home()
    38 bbs()

    2、列表、元组、字典中的深copy、浅copy如何理解?

    import copy
    name1 = ['a','b',['m','n'],'c']
    name2 = copy.copy(name1)
    name3 = copy.deepcopy(name1)
    print(name1)
    print(name2)
    print(name3)
    
    name1[0] = 'h'
    name1[2][0] = 'M'
    print(name1)
    print(name2)
    print(name3)
    结果:
    ['a', 'b', ['m', 'n'], 'c']
    ['a', 'b', ['m', 'n'], 'c']
    
    ['h', 'b', ['M', 'n'], 'c']
    ['a', 'b', ['M', 'n'], 'c']
    ['a', 'b', ['m', 'n'], 'c']

     

     

     
  • 相关阅读:
    Python在函数中使用*和**接收元组和列表
    nvidia-smi实时刷新并高亮显示状态
    linux docker
    linux Vue+nginx+django 实现前后端分离
    linux mysql 主从复制
    linux redis
    linux mysql主从复制
    linux python虚拟环境 相关的
    linux dns
    2 linux vim sudo 文件权限
  • 原文地址:https://www.cnblogs.com/wengxq/p/7252492.html
Copyright © 2020-2023  润新知