What's the best way to minimize the need of run time checks?
BartC
bc at freeuk.com
Fri Aug 12 19:57:53 EDT 2016
On 12/08/2016 23:12, Lawrence D’Oliveiro wrote:
> On Friday, August 12, 2016 at 9:39:11 PM UTC+12, BartC wrote:
>
>> 'year' has been spelled wrongly
>
> That’s why Python has __slots__.
OK. So when I said:
>
> My example was specifically about attribute names where pre-declaring
> the set of allowed attributes would not be onerous, would be usefully
> self-documenting, and would allow more errors to be picked up.
it turns out someone implemented just that!
Although it doesn't sound like Python to bolt-on some random feature
that reins in its dynamic capabilities. Most people here seem to be
against limiting the language in any way.
> (Of course the design of Python makes that impractical because it
> would
> require the byte-code compiler to see inside imported modules before
> execution is commenced.)
And the scheme I had in mind would detect the problem at compile-time.
The '__slots__' feature can't do that (unless someone uses a very clever
'linter'), but it will actually detect an attribute mismatch at runtime
instead of silently creating an extraneous one.
--
Bartc
More information about the Python-list
mailing list