python--有參裝飾器、迭代器

來源:互聯網
上載者:User

標籤:star   art   一個   import   print   utf-8   har   dfs   input   

import time
import random
from functools import wraps

def timmer(func):
    @wraps(func)
    def wrapper():
        # wrapper.__doc__=func.__doc__
        start_time = time.time()
        func()
        stop_time=time.time()
        print(‘run time is %s‘ %(stop_time-start_time))

    # wrapper.__doc__=func.__doc__
    return wrapper

@timmer
def index():
    ‘index function‘
    time.sleep(3)
    print(‘welecome to index page‘)

print(index.__doc__)
# index()
# print(index.__doc__)








# def foo():
#     ‘foo function------------>‘
#     pass
#
# print(help(foo))





# def foo():
#     ‘foo fuction‘
#     print(foo.__doc__)
#     foo.__doc__=‘asdfasdfasdfasdfsadfasdfasdfsadfasdfasfas‘
#     pass

# print(help(foo))

# print(foo.__doc__)
# foo.__doc__=‘abcdefg‘
# print(foo.__doc__)
# print(help(foo))
#
# foo()
# print(foo.__doc__)

 

有參裝飾器

db_path=r‘C:\Users\Administrator\PycharmProjects\python5期\day8\db.txt‘

login_dic={
    ‘user‘:None,
    ‘status‘:False,
}
# def deco(auth_type=‘file‘):
#     def auth(func):
#         def wrapper(*args,**kwargs):
#             if auth_type == ‘file‘:
#                 if login_dic[‘user‘] and login_dic[‘status‘]:
#                     res = func(*args, **kwargs)
#                     return res
#
#                 name=input(‘your name: ‘)
#                 password=input(‘your password: ‘)
#                 with open(db_path,‘r‘,encoding=‘utf-8‘) as f:
#                     user_dic=eval(f.read())
#
#                 if name in user_dic and password == user_dic[name]:
#                         print(‘login ok‘)
#                         login_dic[‘user‘]=name
#                         login_dic[‘status‘]=True
#                         res=func(*args,**kwargs)
#                         return res
#                 else:
#                     print(‘login err‘)
#             elif auth_type == ‘ldap‘:
#                 print(‘ldap認證方式‘)
#             elif auth_type == ‘mysql‘:
#                 print(‘mysql認證方式‘)
#             else:
#                 print(‘不知到的認證方式‘)
#         return wrapper
#     return auth
#
# @deco(auth_type=‘abc‘) #@auth #index=auth(index)
# def index():
#     print(‘welecome to index‘)
#
# @deco(auth_type=‘ldap‘)
# def home(name):
#     print(‘welecome %s to home page‘ %name)
#
#
# index()
#
# home(‘egon‘)

 

 

def deco(auth_type=‘file‘):
    def auth(func):
        def wrapper(*args,**kwargs):
            if auth_type == ‘file‘:
                print(‘檔案的認證方式‘)
            elif auth_type == ‘ldap‘:
                print(‘ldap認證方式‘)
            elif auth_type == ‘mysql‘:
                print(‘mysql認證方式‘)
            else:
                print(‘不知到的認證方式‘)
        return wrapper
    return auth

@deco(auth_type=‘abc‘) #@auth #index=auth(index)
def index():
    print(‘welecome to index‘)

@deco(auth_type=‘ldap‘)
def home(name):
    print(‘welecome %s to home page‘ %name)

index()

home(‘egon‘)

 

#緩衝多個不同網站的內容:
#思路:hash每個url,用得到的值做成檔案名稱,一個網站一個檔案名稱,
# 然後每次根據傳進來的url進行hash得到的結果去尋找檔案

 

 

 

迭代器
  • 迭代:
    1.  重複
    2. 下一次重複是基於上一次的結果

‘‘‘
python為了提供一種不依賴於索引的迭代方式,
python會為一些對象內建__iter__方法
obj.__iter__稱為可迭代的對象
‘‘‘

obj.__iter__() 得到的結果就是迭代器

得到的迭代器:既有__iter__又有一個__next__方法

# d={‘a‘:1,‘b‘:2,‘c‘:3}
#
# i=d.__iter__() #i叫迭代器
# print(i)
# print(i.__next__())
# print(i.__next__())
# print(i.__next__())
# print(i.__next__()) #StopIteration

 

迭代器的優點:

  • 1:提供了一種不依賴於索引的取值方式
  • 2:惰性計算。節省記憶體

迭代器的缺點:

  • 1:取值不如按照索引取值方便
  • 2:一次性的。只能往後走不能往前退
  • 3:無法擷取長度

# for item in l: #i=l.__iter__()
#     print(item)

python--有參裝飾器、迭代器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.