理解managed attribute||New-style Classes||descriptor in Python

  • Source

    Description about descriptor in Python refers managed attribute often.

    So, what the words managed attribute means.

  • Chapter 38 《Learning Python, 5th Edition by Lutz, Mark》

    Managed Attributes expands on the attribute interception techniques.

  • Why Manage Attributes

    Normally, attributes are simply names for objects, a person’s name attribute, for example, might be a simple string, fetched and set with basic attribute syntax:

    person.name
    person.name = value 
    

    In most cases, the attribute lives in the object itself, or is inherited from a class from a class from which it derives.

  • 前情回顧

    managed attribute 是New-style Classes 的衍生概念。

    Useful information on creating and using new-style classes:

  • Unifying types and classes in Python2.2

    PEPs are not designed to be tutorials, and the PEPs(252, 253) describing the type/class unification are sometimes hard to read. That’s where this paper comes in: It introduces the key elements of the type/class unification for the average for the average Python programmer.

  • PEP 252 – Making Types Look More Like Classes

    This PEP proposes changes to the introspection API for types that makes them look more like classes, and their instances more like class instances. For example, type(x) will be equivalent to x.__class__ for most built-in types. When C is x.__class__, x.meth(a) will generally be quivalent to C.meth(x, a), and C.__dict__ contains x’s methods and other attributes.

    This PEP also introduces a new approach to specifying attributes, using attribute descriptor, or descriptors for short.

    Descriptors unify and generalize several different common mechanisms used for describing attributes: a descriptor can describe a method, a typed filed in the object structure, or a generalized attribute represented by getter and setter functions.

    Based on the generalized descriptor API, this PEP also introduces a way to declare class methods and static methods.

  • Introduction

    One of the Python’s oldest language warts is the difference between classes and types. For example, you can’t directly subclass the dictionary type, and the introspection interface for finding out what methods and instance variables an object has is different for types and for classes.

    Healing the class/type split is a big effort, because it affects many aspects of how Python is implemented. This PEP concerns itself with making the introspection API for types look the same as that for classes. Other PEPs will propose making classes look more like types, and subclassing from built-in types; these topics are not on the table for this PEP.

  • Introspection APIs

    Introspection concerns itself with finding out what attributes an object has. Python’s very general getattr/setattr API makes it impossible to guarantee that there always is a way to get a list of all attributes supported by a specific object, but in practice two conventions have appreared that together work for almost all objects. I’ll call them the class-based introspection API and the type-based introspection API; class API and type API for short.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章