Possible PEP: Improve classmethod/staticmethod syntax

Jeff Epler jepler at unpythonic.net
Wed Jun 4 22:22:57 CEST 2003


On Wed, Jun 04, 2003 at 12:31:12PM -0600, Steven Taschuk wrote:
> If we do want a tidier syntax for properties,
> 
>     class myclass(object):
>         property foo:
>             """docstring"""
>             def get(self):
>                 # ...
>             def set(self, value):
>                 # ...
>             def delete(self):
>                 # ...

Can't you write this as
    class C(object):
        class foo(Property):
            """Return the answer to the ultimate question of life, the universe, and everything"""
            def get(self): return 42
given the proper definition of Property? (the trick is that the metaclass
returns a 'property' object instead of a 'Property' subclass, except
when constructing the Property class itself)

class PropertyMeta(type):
    def __new__(typ, name, bases, ns):
        if name != "Property":
            return property(
                ns.get("get", None),
                ns.get("set", None),
                ns.get("del", None),
                ns.get("__doc__", None))
        return super(typ, PropertyMeta).__new__(typ, name, bases, ns)

class Property(object):
    __metaclass__ = PropertyMeta





More information about the Python-list mailing list