SlideShare a Scribd company logo
Descriptors
  jss 2011-07-07
What can you do
inside a class suite?
@c_decorator1
              @c_decorator0(foo=23)
              class Foo(object):
                  __metaclass__ = MetaClass
                  wenglo = 0
                  garply, corge = mktuple(qux)
                  @staticmethod
                  def fred(): pass
                  def __new__(klass): pass

All In One:       @classmethod
                  def wilma(klass): pass
                  def __init__(self, *_): pass
                  @f_decorator
                  def foo(self, *_): pass
                  @property
                  def bar(self): pass
                  @bar.setter
                  def set_bar(self, v): pass
                  helga = Descriptor()
                  print locals()
@c_decorator1
              @c_decorator0(foo=23)
              class Foo(object):
                  __metaclass__ = MetaClass
                  wenglo = 0
                  garply, corge = mktuple(qux)
                  @staticmethod
                  def fred(): pass
                  def __new__(klass): pass

All In One:       @classmethod
                  def wilma(klass): pass
                  def __init__(self, *_): pass
                  @f_decorator
                  def foo(self, *_): pass
                  @property
                  def bar(self): pass
                  @bar.setter
                  def set_bar(self, v): pass
                  helga = Descriptor()
                  print locals()
Descriptor Protocol

•   Descriptors are customized Object Attributes

•   An object with any of these defined is a descriptor:

     __get__(self, obj, type=None)

     __set__(self, obj, val)

     __delete__(self, obj)
Generic Descriptor Examples

class Foo(object):

    def wenglo(self):      f = Foo()
        return self.qux    f.wenglo()
    @classmethod           Foo.foo()
    def foo(klass):        Foo.wenglo()
        return klass.bar
    @staticmethod
      def buz():
          return 42
“Normal” attribute access

class Foo(object): pass

f = Foo()

f.bla

# implemented in object.__getattribute__:

== type(f).__dict__[‘bla’].__get__(f, type(f))
Demo


         show some code:

check out ListProperty in postamt.py
Implementing property()
# by Raymond Hettinger: http://users.rcn.com/python/download/Descriptor.htm
class property(object):
    def __init__(self, fget=None, fset=None, fdel=None, doc=None):
        self.fget = fget
        self.fset = fset
        self.fdel = fdel
        self.__doc__ = doc

    def __get__(self, obj, objtype=None):
        if obj is None:
            return self
        if self.fget is None:
            raise AttributeError, "unreadable attribute"
        return self.fget(obj)

    def __set__(self, obj, value):
        if self.fset is None:
            raise AttributeError, "can't set attribute"
        self.fset(obj, value)

    def __delete__(self, obj):
        if self.fdel is None:
            raise AttributeError, "can't delete attribute"
        self.fdel(obj)

More Related Content

PDF
Advanced Python, Part 1
PDF
2 + 2 = 5: Monkey-patching CPython with ctypes to conform to Party doctrine
PDF
Introduction to ad-3.4, an automatic differentiation library in Haskell
PDF
Why you-dont-need-design-patterns-in-python
PDF
purely_functional_play_framework_application
PDF
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PDF
ES6 General Introduction
PDF
Python decorators
Advanced Python, Part 1
2 + 2 = 5: Monkey-patching CPython with ctypes to conform to Party doctrine
Introduction to ad-3.4, an automatic differentiation library in Haskell
Why you-dont-need-design-patterns-in-python
purely_functional_play_framework_application
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
ES6 General Introduction
Python decorators

What's hot (20)

PPTX
Python Metaclasses
PDF
CodeFest 2010. Иноземцев И. — Fantom. Cross-VM Language
PDF
ECMA 入门
PPT
Testing for share
PDF
Advanced python
PDF
Fantastic DSL in Python
KEY
ddd+scala
PPTX
20170113 julia’s type system and multiple dispatch
PPTX
Namespaces
PDF
Declarative Data Modeling in Python
PDF
JavaScript for PHP developers
PPTX
Python decorators
PPTX
Decorators Explained: A Powerful Tool That Should Be in Your Python Toolbelt.
ODP
Decorators in Python
PDF
Swift 3 Programming for iOS : extension
PDF
Hey! There's OCaml in my Rust!
PDF
TypeScript Introduction
PDF
Mirror, mirror on the wall - Building a new PHP reflection library (Nomad PHP...
PDF
Exploring Ceylon with Gavin King - JUG BB Talk - Belrin 2014
Python Metaclasses
CodeFest 2010. Иноземцев И. — Fantom. Cross-VM Language
ECMA 入门
Testing for share
Advanced python
Fantastic DSL in Python
ddd+scala
20170113 julia’s type system and multiple dispatch
Namespaces
Declarative Data Modeling in Python
JavaScript for PHP developers
Python decorators
Decorators Explained: A Powerful Tool That Should Be in Your Python Toolbelt.
Decorators in Python
Swift 3 Programming for iOS : extension
Hey! There's OCaml in my Rust!
TypeScript Introduction
Mirror, mirror on the wall - Building a new PHP reflection library (Nomad PHP...
Exploring Ceylon with Gavin King - JUG BB Talk - Belrin 2014
Ad

Viewers also liked (20)

PPTX
Monaqasat.com ArabNET presentation
PPT
Rest breaks
PPT
How long is the working week
PPTX
Ejercicio2.carlos lozano
PPTX
Missd
PDF
Proyecto de vida
DOCX
cvnophotoA
PDF
Preguntas deider
PPTX
Geografia
PDF
La Pugliese | Analisis territorial
DOCX
Ejemplo de estudio de mercado
PPTX
ตัวอย่างชิ้นงาน P5 2-58
PPTX
Ideologías políticas
PPTX
Expo1 adm finac-p
PDF
STANDARD FOR FABRICS (SUITING) PER SNI
PPT
Textile fabrics analysis
PPTX
be proud to be an indian on independance day VRVP
DOCX
ForbushSchoolResumeandcv
PDF
101910 open letter_to_news_media
PPTX
Smart phones final
Monaqasat.com ArabNET presentation
Rest breaks
How long is the working week
Ejercicio2.carlos lozano
Missd
Proyecto de vida
cvnophotoA
Preguntas deider
Geografia
La Pugliese | Analisis territorial
Ejemplo de estudio de mercado
ตัวอย่างชิ้นงาน P5 2-58
Ideologías políticas
Expo1 adm finac-p
STANDARD FOR FABRICS (SUITING) PER SNI
Textile fabrics analysis
be proud to be an indian on independance day VRVP
ForbushSchoolResumeandcv
101910 open letter_to_news_media
Smart phones final
Ad

Similar to Descriptor Protocol (20)

KEY
DjangoCon US 2011 - Monkeying around at New Relic
KEY
Djangocon11: Monkeying around at New Relic
PDF
Object Orientation vs Functional Programming in Python
PDF
Pybelsberg — Constraint-based Programming in Python
PPTX
Python_Unit_2 OOPS.pptx
PDF
Python's magic methods
PPTX
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
PDF
Object_Oriented_Programming_Unit3.pdf
KEY
Pyimproved again
PDF
Python decorators (中文)
PDF
Design of OO language
PDF
Dive into Python Class
PDF
Python magicmethods
PDF
Object Trampoline: Why having not the object you want is what you need.
PDF
用Tornado开发RESTful API运用
PDF
Introduction to Python
PPTX
Python_Object_Oriented_Programming.pptx
PPT
Introduction to Python - Part Three
PDF
Python Descriptors Demystified
DjangoCon US 2011 - Monkeying around at New Relic
Djangocon11: Monkeying around at New Relic
Object Orientation vs Functional Programming in Python
Pybelsberg — Constraint-based Programming in Python
Python_Unit_2 OOPS.pptx
Python's magic methods
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
Object_Oriented_Programming_Unit3.pdf
Pyimproved again
Python decorators (中文)
Design of OO language
Dive into Python Class
Python magicmethods
Object Trampoline: Why having not the object you want is what you need.
用Tornado开发RESTful API运用
Introduction to Python
Python_Object_Oriented_Programming.pptx
Introduction to Python - Part Three
Python Descriptors Demystified

More from rocketcircus (8)

PDF
Pytables
PDF
Descriptor Protocol
PDF
Python Academy
PDF
intro to scikits.learn
PDF
AWS Quick Intro
PDF
PyPy 1.5
PDF
Message Queues
PDF
Rocket Circus on Code Review
Pytables
Descriptor Protocol
Python Academy
intro to scikits.learn
AWS Quick Intro
PyPy 1.5
Message Queues
Rocket Circus on Code Review

Recently uploaded (20)

PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
Cloud computing and distributed systems.
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Machine learning based COVID-19 study performance prediction
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
MYSQL Presentation for SQL database connectivity
Digital-Transformation-Roadmap-for-Companies.pptx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
“AI and Expert System Decision Support & Business Intelligence Systems”
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Chapter 3 Spatial Domain Image Processing.pdf
Cloud computing and distributed systems.
MIND Revenue Release Quarter 2 2025 Press Release
Big Data Technologies - Introduction.pptx
Programs and apps: productivity, graphics, security and other tools
Network Security Unit 5.pdf for BCA BBA.
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Machine learning based COVID-19 study performance prediction
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Review of recent advances in non-invasive hemoglobin estimation
Agricultural_Statistics_at_a_Glance_2022_0.pdf

Descriptor Protocol

  • 1. Descriptors jss 2011-07-07
  • 2. What can you do inside a class suite?
  • 3. @c_decorator1 @c_decorator0(foo=23) class Foo(object): __metaclass__ = MetaClass wenglo = 0 garply, corge = mktuple(qux) @staticmethod def fred(): pass def __new__(klass): pass All In One: @classmethod def wilma(klass): pass def __init__(self, *_): pass @f_decorator def foo(self, *_): pass @property def bar(self): pass @bar.setter def set_bar(self, v): pass helga = Descriptor() print locals()
  • 4. @c_decorator1 @c_decorator0(foo=23) class Foo(object): __metaclass__ = MetaClass wenglo = 0 garply, corge = mktuple(qux) @staticmethod def fred(): pass def __new__(klass): pass All In One: @classmethod def wilma(klass): pass def __init__(self, *_): pass @f_decorator def foo(self, *_): pass @property def bar(self): pass @bar.setter def set_bar(self, v): pass helga = Descriptor() print locals()
  • 5. Descriptor Protocol • Descriptors are customized Object Attributes • An object with any of these defined is a descriptor: __get__(self, obj, type=None) __set__(self, obj, val) __delete__(self, obj)
  • 6. Generic Descriptor Examples class Foo(object): def wenglo(self): f = Foo() return self.qux f.wenglo() @classmethod Foo.foo() def foo(klass): Foo.wenglo() return klass.bar @staticmethod def buz(): return 42
  • 7. “Normal” attribute access class Foo(object): pass f = Foo() f.bla # implemented in object.__getattribute__: == type(f).__dict__[‘bla’].__get__(f, type(f))
  • 8. Demo show some code: check out ListProperty in postamt.py
  • 9. Implementing property() # by Raymond Hettinger: http://users.rcn.com/python/download/Descriptor.htm class property(object): def __init__(self, fget=None, fset=None, fdel=None, doc=None): self.fget = fget self.fset = fset self.fdel = fdel self.__doc__ = doc def __get__(self, obj, objtype=None): if obj is None: return self if self.fget is None: raise AttributeError, "unreadable attribute" return self.fget(obj) def __set__(self, obj, value): if self.fset is None: raise AttributeError, "can't set attribute" self.fset(obj, value) def __delete__(self, obj): if self.fdel is None: raise AttributeError, "can't delete attribute" self.fdel(obj)