• 面向对象简介


    object的方法

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

    比较的特殊方法:

    特殊方法 使用 描述
    __lt__(self.other) x<y 如果x<y,则返回true
    __le__(self.other) x<=y 如果x<=y,返回true
    __eq__(self.other) x==y 如果x==y,返回true
    __ne__(self.other) x!=y 如果x!=y,返回true
    __ge__(self.other) x>=y 如果x>=y,返回true
    __gt__(self.other) x>y 如果x>y,返回true

    基本的特殊方法:

    特殊方法 使用 描述
    __bool__(self) bool(x) 如果提供,就返回x的真值, 对if x:...是有用的
    __format__(self, format_spec) "{0}".format(x) 为自定义类提供str.format()支持
    __hash__(self) hash(x) 如果提供,那么x可用作字典的键或存放在集合中
    __init__(self, args) x = X(args) 对象初始化时调用
    __new__(cls, args) x = X(args) 创建对象时调用
    __repr__(self) repr(x) 返回x的字符串表示,在可能的地方eval(repr(x))==x
    __repr__(self) ascii(x) 仅使用ASCII字符返回x的字符串表示,对象的repr()结果可以通过eval()转换成对象,而__str__()不可以
    __str__(self) str(x) 返回x适合阅读的字符串表示形式
    __len__(self) len(x) 返回x的长度

    数值型与位逻辑运算的特殊方法:

    特殊方法 使用 特殊方法 使用
    __abs__(self) abs(x) __complex__(self) complex(x)
    __float__(self) float(x) __int__(self) int(x)
    __index__(self) bin(x) oct(x) hex(x) __round__(self,digits) round(x,digits)
    __pos__(self) +x __neg__(self) -x
    __add__(self, other) x + y __sub__(self, other) x - y
    __iadd__(self, other) x += y __isub__(self, other) x -= y
    __radd__(self, other) y + x __rsub__(self, other) y - x
    __mul__(self, other) x * y __mod__(self, other) x % y
    __imul__(self, other) x *= y __imod__(self, other) x %= y
    __rmul__(self, other) y * x __rmod__(self, other) y % x
    __floordiv__(self, other) x // y __truediv__(self, other) x / y
    __ifloordiv__(self, other) x //= y __itruediv__(self, other) x /= y
    __rfloordiv__(self, other) y // x __rtruediv__(self, other) y / x
    __divmod_(self, other) divmod(x, y) __rdivmod__(self, other) divmod(y, x)
    __pow__(self, other) x ** y __and__(self, other) x & y
    __ipow__(self, other) x **= y __iand__(self, other) x &= y
    __rpow__(self, other) y ** x __rand__(self, other) y & x
    __xor__(self, other) x ^ y __or__(self, other) x | y
    __ixor__(self, other) x ^= y __ior__(self, other) x |= y
    __rxor__(self, other) y ^ x __ror__(self, other) y | x
    __lshift__(self, other) x << y __rshift__(self, other) x >> y
    __ishift__(self,other) x <<= y __irshift__(self, other) x >>= y
    __rshift__(self, other) y << x __rrshift__(self, other) y >> x
    __invert__(self) ~x    

    组合类型的特殊方法:

    特殊方法 使用 描述
    __getitem__(self, k) y[k] 返回序列y中的第k项或映射y中键为k项的值
    __setitem__(self, k, v) y[k] = v 将序列y中的第k项(或映射y中键为k的项)设置为v
    __delitem__(self, k) del y[k] 删除序列y中的第k项或映射y中键为k的项
    __contains__(self, x) x in y 如果x在序列y中或x是映射y中的键,就返回True
    __iter__(self) for x in y: pass 返回序列y中的项或映射y中键的迭代器
    __len__(self) len(y) 返回y中项的个数
    __reversed__(self) reversed(y) 返回序列y中的项或映射y中键的反向迭代器

     object属性

    '''
    __doc__:打印类的注释信息
    __module__:返回对象的模块
    __class__:输出类名,使用:__class__.__name__
    __init__:构造方法
    __del__:析构方法
    __call__:让对象可以加括号运行
    __dict__:查看类(打印类里的所有属性不包括实例属性)或对象(打印实例属性不包括类属性)中的所有成员
    __metaclass__:定义自己的类被哪个原类创建

    对比:

    1、属性方法@property @xx.setter @xx.deleter   使用xx属性名操作成员方法(私有属性)或需要通过属性访问的其他方法,返回属性值,设置属性值,删除属性。做属性的值验证等。

    2、反射 setatttr()  getattr()  delattr()            使用属性的字符串操作成员属性,成员方法

    ################################################################################
    '''
    Python反射:
    1、hasattr(obj,str)判断一个对象obj里是否有名称是str的方法或属性
       可以判断类或方法里是否有某属性:hasattr(类名/方法名, "__call__")
    2、getattr(obj,str)获取obj对象里名称是str的方法或属性,get完后可以加括号直接调用
    3、setattr(x,y,v)  添加/修改属性或方法,相当于x.y=v,可以直接setattr(类名,k , v)
    4、delattr(obj,str) 删除obj名称是str的属性或方法,可以直接delattr(类名,k,v)
    '''
    ################################################################################
    '''
    属性方法
    '''
    class Dog:
        def __init__(self):
            self.__food = None
        @property
        def eat(self):
            print("eat:", self.__food)
        @eat.setter
        def eat(self, food):
            self.__food = food
        @eat.deleter
        def eat(self):
            del self.__food
    
    d = Dog()
    d.eat
    d.eat = "骨头"
    d.eat
    del d.eat
    d.eat
    
    ############
    # eat: None
    # eat: 骨头
    # AttributeError: 'Dog' object has no attribute '_Dog__food'
    ############

    3、特殊方法 __getitem__ 、 __setitem__ 、 __delitem__  给对象里的字典或列表进行增加、删除、修改等操作。访问方式为obj[xx]

  • 相关阅读:
    Linux/UNIX编程:实现简单 tee 命令
    Java原子变量类需要注意的问题
    一种很有意思的数据结构:Bitmap
    Java实现简单井字棋
    分治算法学习
    使用栈实现表达式求值
    Web安全学习笔记——SQL注入
    【old】Python学习笔记
    函数1
    pycharm(Tip of Day)
  • 原文地址:https://www.cnblogs.com/staff/p/9326881.html
Copyright © 2020-2023  润新知