How to automate accessor definition?
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Sun Mar 21 14:18:57 EDT 2010
On Sun, 21 Mar 2010 16:57:40 +0000, kj wrote:
> Just accessing attributes looks a bit dangerous to me, due to bugs like
> typing
>
> i.typo = 'foo'
>
> when what you meant is
>
> i.type = 'foo'
That's the price you pay for using a dynamic language like Python with no
declarations. But honestly, the price isn't very high, particularly if
you use an editor or IDE with auto-completion. I can't think of the last
time I had an error due to the above sort of mistake.
Besides, is that error really so much more likely than this?
i.type = 'fpo'
when you meant 'foo'? The compiler can't protect you from that error, not
in any language.
> I tried fixing this by mucking with __setattr__, but I didn't hit on a
> satisfactory solution (basically, I couldn't find a good,
> self-maintaining, way to specify the attributes that were OK to set from
> those that weren't). Is there anything built-in?
No.
You could abuse __slots__, but it really is abuse: __slots__ are a memory
optimization, not a typo-checker.
In Python 3.x, you can (untested) replace the class __dict__ with a
custom type that has more smarts. At the cost of performance. This
doesn't work in 2.x though, as the class __dict__ is always a regular
dictionary.
Something like this might work, at some minor cost of performance:
# Untested
def __setattr__(self, name, value):
if hasattr(self, name):
super(MyClassName, self).__setattr__(name, value)
else:
raise TypeError('cannot create new attributes')
Then, in your __init__ method, to initialise an attribute use:
self.__dict__['attr'] = value
to bypass the setattr.
Or you can use something like PyChecker or PyLint to analyse your code
and warm about likely typos.
But really, it's not a common form of error. YMMV.
> Regarding properties, is there a built-in way to memoize them? For
> example, suppose that the value of a property is obtained by parsing the
> contents of a file (specified in another instance attribute). It would
> make no sense to do this parsing more than once. Is there a standard
> idiom for memoizing the value once it is determined for the first time?
Google for "Python memoization cookbook". This will get you started:
http://code.activestate.com/recipes/52201/
Then just apply the memoize decorator to the property getter.
--
Steven
More information about the Python-list
mailing list