PEP318: property as decoration

Sean Ross frobozz_electric at hotmail.com
Wed Jun 11 23:47:55 EDT 2003


"Gerrit Holl" <gerrit at nl.linux.org> wrote in message
> Hmm, that doesn't seem to work. __doc__ is not in my local namespace yet.

Here`s a brittle hack to resolve that issue:

import inspect

def this():
        """returns the calling function"""
        # pull function name from outer scope (one)
        fname = inspect.currentframe(1).f_code.co_name
        try:
                # pull function itself from outer scope of one (two)
                func = inspect.currentframe(2).f_locals[fname]
        except NameError:
                # func is a method, pull function from class dict
                # This is BRITTLE! Cannot guarantee this methods'
                # class is at outer scope of two (three). This requires
                # a better lookup sequence.
                cdict = inspect.currentframe(3).f_locals
                return cdict.get(fname, None)
        return func


Then, inside your method you can do this:

def foo():
        "foo property's doc string"
        def fget(self):
                return self._foo
        def fset(self, value):
                self._foo = value
        def fdel(self):
                del self._foo
        return fget, fset, fdel, this().__doc__

As I say, "this" is a very brittle hack, so don't try this at home (pardon
the pun)








More information about the Python-list mailing list