推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
fanhaipeng0403
V2EX  ›  Python

Python 的 __get__描述符

  •  
  •   fanhaipeng0403 · Nov 24, 2018 · 1916 views
    This topic created in 2861 days ago, the information mentioned may be changed or developed.
    
    
    """"
    
    https://www.jb51.net/article/86749.htm
    https://python3-cookbook.readthedocs.io/zh_CN/latest/c08/p09_create_new_kind_of_class_or_instance_attribute.html
    
    
    """
    class C(object):
        """
       存在了__get__的方法的类称之为描述符类
    
        descriptor 的实例自己访问自己是不会触发__get__,而会触发__call__,只有 descriptor 作为其它类的属性的时候才会触发 __get___
        """
        a = 'abc'
    
        def __get__(self, instance, owner):
            print("__get__() is called", instance, owner)
            return self
    
    
    
    class C2(object):
        # 为了使用一个描述器,需将这个描述器的实例作为类属性放到一个类的定义中.
        d = C()  # descriptor 的实例自己访问自己是不会触发__get__,而会触发__call__,只有 descriptor 作为其它类的属性的时候才会触发 __get___
    
    
    if __name__ == '__main__':
        # 不触发
        c = C()
        print(c.a)
    
        # 触发
        c2 = C2()
        print(c2.d.a)
    
    
    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2437 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 05:12 · PVG 13:12 · LAX 22:12 · JFK 01:12
    ♥ Do have faith in what you're doing.