On 13/08/2016 13:08, BartC wrote:
On 13/08/2016 12:10, Chris Angelico wrote:
On Sat, Aug 13, 2016 at 8:09 PM, BartC <b...@freeuk.com> wrote:

And if subclassing isn't enough, there's no end of stuff you can do
with decorators. Try this:

class Foo:
    @prop
    class demo:
        """Declarative property"""
        def get(self):
            print("Getting %s.demo" % self)
            return 42
        def set(self, val):
            print("Setting %s.demo" % self)
        def delete(self):
            print("Deleting %s.demo" % self)

Yaknow, because @property is just waaaaaaay too clunky, right? :) But
I needed a simple demo. The decorator isn't even all that complicated.

def prop(cls):
    return property(cls.get, getattr(cls, "set"), getattr(cls,
"delete"), cls.__doc__)

(But don't try and explain decorators. I've tried several times to get
my head around them.)

This is a clearer than usual explanation:

 http://thecodeship.com/patterns/guide-to-python-function-decorators/

Not quite simple enough however to figure out how decorators benefit your example! (Maybe an example with and without decorators?)

So if the aim is to keep code readable and understandable to as wide an audience as possible, 'decorators' don't really work. (Perhaps if they are buried somewhere else and only an interface is visible, but then it doesn't matter so much how hairy that buried code is.)

--
Bartc
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to