zoukankan      html  css  js  c++  java
  • python_函数设计

     1 >>> def check_permission(func):
     2 def wrapper(*args,**kwargs):
     3 if kwargs.get('username')!='admin':
     4 raise Exception('Sorry.You are not allowed.')
     5 return func(*args,**kwargs)
     6 return wrapper
     7 
     8 >>> class ReadWriteFile(object):
     9 '''The check_permisson function is used as a decorator'''
    10 @check_permission
    11 def read(self,username,filename):
    12 return open(filename,'r').read()
    13 def write(self,username,filename,content):
    14 open(filename,'a+').write(content)
    15 write=check_permission(write)
    16 
    17 
    18 >>> t=ReadWriteFile()
    19 >>> print('Originally...')
    20 Originally...
    21 >>> print(t.read(username='admin',filename='/Users/c2apple/Desktop/file.txt'))
    22 hellow world my birthday
    23 >>> print('Now,try to write to a file...')
    24 Now,try to write to a file...
    25 >>> t.write(username='admin',filename='/Users/c2apple/Desktop/file.txt',content='
    hello my love is ended')
    26 >>> print('After calling to write....')
    27 After calling to write....
    28 >>> print(t.read(username='admin',filename='/Users/c2apple/Desktop/file.txt'))
    29 hellow world my birthday
    30 hello my love is ended
  • 相关阅读:
    轮播制作
    前端问题总结
    响应式 媒体查询 盒模型
    响应式 字体设置 flex 弹性布局
    C++ STL之set常用指令
    SRM468
    SRM470
    置换及其应用专题
    C++ STL之map常用指令
    C++ STL之pair常用指令
  • 原文地址:https://www.cnblogs.com/cmnz/p/6902081.html
Copyright © 2011-2022 走看看