What's the best way to minimize the need of run time checks?

Chris Angelico rosuav at gmail.com
Sun Aug 14 00:20:54 EDT 2016


On Sun, Aug 14, 2016 at 2:13 PM, Steven D'Aprano
<steve+python at pearwood.info> wrote:
> What you should be doing is comparing the types of record *instances*
> instead:
>
> py> x = record('Spam', 'breakfast lunch dinner')('spam',
> ...     'spam and eggs', 'spam and eggs and spam with a side-dish of spam')
> py> y = record('Date', 'year month day')(1999, 'August', 14)
> py> type(x), type(y)
> (<class '__main__.record.<locals>.Inner'>,
> <class '__main__.record.<locals>.Inner'>)
>
>
> Hmmm. I didn't expect that. I expected to see the names of the classes:
>
> py> type(x).__name__, type(y).__name__
> ('Spam', 'Date')
>
>
> If I was publishing this as a polished product, I'd want to fix that.

But at this point, you're at the level of minor tweaks, not core
functionality - and more importantly, proof of concept is successful.
You have a working class factory, which is downright *impossible* in
many languages.

ChrisA



More information about the Python-list mailing list