Declarative properties
Artur Siekielski
artur.siekielski at gmail.com
Thu Oct 11 07:48:18 EDT 2007
Hi.
I would like to have declarative properties in Python, ie. something
like slots definitions in defclass in Common Lisp. It seems that even
Java will have it, using a library ( https://bean-properties.dev.java.net/
).
I know about 'property' function in Python, but it's normal usage
isn't declarative, because I have to code imperatively getters and
setters:
class Person(object):
def __init__(self, name):
self._name = name
def _get_name(self):
return self._name
def _set_name(self, new_name):
self._name = new_name
name = property(_get_name, _set_name)
I would like to have something like that:
class Person(object):
name = property('_name')
I assume that this causes "generation" of instance field '_name' and
default getters and setters. And if I would like to customize (g|
s)etters, I would write them by hand, or, better, use more declarative
approach (like @NotNull annotations in Java version).
I could probably code a solution to my problem with help of wonderful
introspection functionalities. But I'm looking for a standard and/or
proven way of doing this. Maybe some PEP is being prepared for this?
Regards,
Artur
More information about the Python-list
mailing list