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__)

Which level is this nearer to, human thoughts or cpu codes?

My knowledge of Python isn't advanced enough to understand what this does. But if you would be good enough to explain or to show the end results (what it makes possible) then I could get an opinion as to how hard it might be with less dynamic features.

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

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

Reply via email to