zoukankan      html  css  js  c++  java
  • 特殊方法和装饰器

      1 # 多继承
      2 class A:
      3     def show(self):
      4         print('AAAA')
      5 
      6 class B:
      7     def fun(self):
      8         print('BBBB')
      9 
     10 class C(B,A):
     11     pass
     12 
     13 x = C()
     14 
     15 
     16 # 类的特殊方法
     17 '''
     18 类属性:
     19 __dict__ : 类的属性(包含一个字典,由类的数据属性组成)
     20 __doc__ :类的文档字符串
     21 __name__: 类名
     22 '''
     23 x = 'a\nb'
     24 #print(repr(x))
     25 #实例调用:
     26 '''
     27 __init__    初始化
     28 __repr__    c1
     29 __str__     print (c1 )(如果类里面先定义了__repr__的话print x时也会返回对应的
     30 __call__    c1()   使实例可被调用
     31 '''
     32 class Rectangle:
     33     def __init__(self,width,height):
     34         self.width = width
     35         self.height = height
     36     def __str__(self):
     37         return '宽为%s,高为%s'%(self.width,self.height)
     38     def __repr__(self):
     39         return '面积为%s'%self.area()
     40     def __call__(self):
     41         return '哈哈哈 '
     42     def area(self):
     43         return self.width*self.height
     44     def __add__(self,other):
     45         if isinstance(other,Rectangle):
     46             return self.area()+other.area()
     47     
     48 c1 = Rectangle(3,4) 
     49 c2 = Rectangle(5,6)
     50 # 运算符魔法方法:
     51 '''
     52 __add__(self,other)     x+y
     53 __sub__(self,other)     x-y 
     54 __mul__(self,other)     x*y  
     55 __mod__(self,other)    x%y
     56 __iadd__(self,other)    x+=y
     57 __isub__(self,other)    x-=y 
     58 __radd__(self,other)    y+x
     59 __rsub__(self,other)     y-x 
     60 __imul__(self,other)    x*=y 
     61 __imod__(self,other)    x%=y
     62 '''
     63 # 装饰器
     64 '''
     65 @property 装饰过的函数返回的不再是一个函数,而是一个property对象
     66           装饰过后的方法不再是可调用的对象,可以看做数据属性直接访问。
     67 @staticmethod 把没有参数的函数装饰过后变成可被实例调用的函数,
     68              函数定义时是没有参数的。
     69 @classmethod 把装饰过的方法变成一个classmethod类对象,既能能被类调用又能被实例调用。
     70  注意参数是cls代表这个类本身。而是用实例的方法只能被实例调用。             
     71 '''
     72 class Rectangle:
     73     def __init__(self,width,height):
     74         self.width = width
     75         self.height = height
     76     @property
     77     def area(self):
     78         return self.width*self.height
     79     @staticmethod
     80     def fun():
     81         return 'xxxxxx'
     82     @classmethod
     83     def show(cls):
     84         print(cls)
     85         return 'YYYY'
     86 c1 = Rectangle(3,4)
     87 def fun1(ff):
     88     def fun2(y):
     89         return ff(y)+100
     90     return fun2
     91 
     92 @fun1    #ff = fun1(ff)     # x=ff
     93 def ff(y):
     94     return y*y
     95 
     96 def filterarg(x):
     97     def fit(*arg):
     98         if len(arg) == 0:
     99             return 0
    100         for i in arg:
    101             if not isinstance(i,int):
    102                 return 0
    103         return x(*arg)
    104     return fit
    105 
    106 #@filterarg
    107 def sums(*arg):
    108     return sum(arg)
    109 
    110 filterarg(sums)(3,4,5)
    111 
    112 @filterarg
    113 def average(*arg):
    114     print(arg)
    115     return sum(arg)/len(arg)
    View Code
  • 相关阅读:
    用python分析1225万条淘宝数据,终于搞清楚了我的交易行为
    Python 中 3 个不可思议的返回
    2020年最新的过某宝滑块验证技术,Python大牛轻松搞定技术难题
    改改Python代码,运行速度还能提升6万倍
    Python的10个神奇的技巧
    Python 在线免费批量美颜,妈妈再也不用担心我 P 图两小时啦
    全程干货,requests模块与selenium框架详解
    Python-选择器Xpath,Css,Re
    Python-Django 模型层-多表查询-2
    Python-Django 模型层-多表查询
  • 原文地址:https://www.cnblogs.com/lajiao/p/7739952.html
Copyright © 2011-2022 走看看