[Python-ideas] A user story concerning things knowing their own names
Ian Bicking
ianb at colorstudy.com
Wed Mar 16 18:13:14 CET 2011
I'll note this general problem is also present in any of the declarative
ORMs, which use silly hacks to tell descriptors their name. Like you have:
class Table(ORM):
name = StringColumn()
Another case where I've noticed a problem is any kind of descriptor that
needs its own storage; the name of the property gives a possible stable
namespace for the value, but without the name you either have to pass in a
name or the storage area becomes volatile. For instance, a read-only
descriptor: http://svn.colorstudy.com/home/ianb/recipes/setonce.py
You can solve this in, e.g., the ORM class by doing things when a class is
created -- but it requires very specific cooperation between the class and
the descriptor. Everyone really does it different ways.
One could imagine an extension of the descriptor protocol, where on class
creation you called something like attr.__addtoclass__(cls, name) (for all
attributes of the class that define that method) -- which if you just want
the name you'd simply save that name in your object and return self.
On Wed, Mar 16, 2011 at 4:28 AM, Greg Ewing <greg.ewing at canterbury.ac.nz>wrote:
> I just experienced an obscure bug resulting from copying
> and pasting an overridable_property and forgetting to
> change the passed-in name:
>
> content_size = overridable_property('size',
> "Size of the content area.")
>
> which should have been
>
> content_size = overridable_property('content_size',
> "Size of the content area.")
>
> This was quite difficult to track down, because the result
> was to effectively make it an alias of *another* property
> I have called 'size'. They happen to be near enough to the same
> thing that the error went unnoticed for quite some time.
>
> If I could write my overridable_property declarations
> without having to repeat the name, this kind of thing would
> not be able to happen.
>
> --
> Greg
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110316/537cfa4e/attachment.html>
More information about the Python-ideas
mailing list