zoukankan      html  css  js  c++  java
  • python常用魔术方法概览

    构造和初始化

    __init__(self, args)

    构造函数

    __new__(cls)

    传入的是类实例

    __del__(self)

    析构函数,调用 del cls 时会被调用

    属性访问控制

    __getattr__(self, name)

    如果属性已经定义了那么不会再执行__getattr__()了,而是直接通过访问实例字典返回结果,__getattr__()只在访问未定义的属性时被触发

    __setattr__(self, name, value)

    直接给属性赋值 cls.name = value, 如果该函数内部内部使用self.name = value 时会产生“无限递归”的错误,正确的方式应该是

    1
    2
    def (self, name, value):
    self.__dict__[name] = value

    __delattr__(self, name)

    del cls.name 时会被调用

    描述器对象

    __set__(self, cls, value)

    cls.innercls = value 用于类中的其他类对象赋值

    __get__(self, cls)

    cls.innercls 返回类中其他对象传回的值

    __delete__(self, cls)

    del cls.innercls

    综合实例
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    class Meter(object):
    '''Descriptor for a meter.'''
    def __init__(self, value=0.0):
    self.value = float(value)
    def __get__(self, instance, owner):
    return self.value
    def __set__(self, instance, value):
    self.value = float(value)

    class Foot(object):
    '''Descriptor for a foot.'''
    def __get__(self, instance, owner):
    return instance.meter * 3.2808
    def __set__(self, instance, value):
    instance.meter = float(value) / 3.2808

    class Distance(object):
    大专栏  python常用魔术方法概览pan class="line"> meter = Meter()
    foot = Foot()

    d = Distance()
    print d.meter, d.foot
    d.meter = 1
    print d.meter, d.foot # 1.0 3.2808
    d.meter = 2
    print d.meter, d.foot # 2.0 6.5616

    自定义容器(Container)

    __len__(self)

    len(con) 返回容器长度

    __setitem__(self, name, value)

    con[name] = value 直接下标赋值

    __getitem__(self, name)

    con[name] 直接下标访问

    __delitem__(name)

    del con[name] 删除下标

    __iter__(self)

    使得容器支持迭代器方式访问 for x in con

    __contains__(self, item)

    name in con 可以返回布尔值

    __missing__(self, name)

    容器中没有name时会被调用

    上下文管理

    with 关键字可实现上下文(环境)管理

    __enter__(self)

    进入环境时触发

    __exit__(self, exception_type, exception_value, traceback)

    退出环境时触发,一般用来关闭资源

    运算符重载

    __eq__(self, other)

    重载 =

    __ne__(self, other)

    重载 !=

    __lt__(self, other)

    重载 <

    __gt__(self, other)

    重载 >

    __le__(self, other)

    重载 <=

    __ge__(self, other)

    重载 >=

    其他

    __str__(self)

    print(cls)str(cls) 时被调用,必须返回字符str类型

    __repr__(self)

    对实例使用repr()时调用。str()repr()都是返回一个代表该实例的字符串,主要区别在于: str()的返回值要方便人来看,而repr()的返回值方便计算机看。

    __call__(self, *args, **kwargs)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    class XClass:
    def __call__(self, a, b):
    return a + b

    def add(a, b):
    return a + b

    x = XClass()
    print 'x(1, 2)', x(1, 2)
    print 'callable(x)', callable(x) # True
    print 'add(1, 2)', add(1, 2)
    print 'callable(add)', callable(add) # True

    https://segmentfault.com/a/1190000007256392#articleHeader3

  • 相关阅读:
    HDFS常用命令总结
    mac 安装zmap
    使用scrapy-redis搭建分布式爬虫环境
    xpath选择兄弟节点、返回上一级和选择多个属性
    什么是套接字(Socket)
    socket编程——一个简单的例子
    python之 __getattr__、__getattr__、__getitem__、__setitem__ 使用
    如何快速转载文章
    如何让类也变成可以迭代的对象
    leecode 第二题 简单
  • 原文地址:https://www.cnblogs.com/lijianming180/p/12275658.html
Copyright © 2011-2022 走看看