[Edu-sig] a Circle type with radius, area both properties (fixed)

kirby urner kirby.urner at gmail.com
Thu Oct 20 22:49:09 EDT 2016


I swap in the pure Python emulator for the property type
as a part of explaining how these decorators work:


On Thu, Oct 20, 2016 at 7:45 PM, kirby urner <kirby.urner at gmail.com> wrote:


> from model_property import Property as property
>>
>>

# -*- coding: utf-8 -*-
"""
Created on Thu Oct 20 15:45:20 2016

@author: Python.org

Emulates the built-in property type in pure Python

"""

class Property(object):
    "Emulate PyProperty_Type() in Objects/descrobject.c"

    def __init__(self, fget=None, fset=None, fdel=None, doc=None):
        self.fget = fget
        self.fset = fset
        self.fdel = fdel
        if doc is None and fget is not None:
            doc = fget.__doc__
        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)

    def getter(self, fget):
        return type(self)(fget, self.fset, self.fdel, self.__doc__)

    def setter(self, fset):
        return type(self)(self.fget, fset, self.fdel, self.__doc__)

    def deleter(self, fdel):
        return Property(self.fget, self.fset, fdel, self.__doc__)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/edu-sig/attachments/20161020/c84c662d/attachment-0001.html>


More information about the Edu-sig mailing list