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

BartC bc at freeuk.com
Sun Aug 14 06:49:14 EDT 2016


On 14/08/2016 05:20, Chris Angelico wrote:
> 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.

Well, it's using exec(). So it is generating new program code at 
runtime. That is possible in quite a few languages, even C.

Python is much better however in being able to integrate the results 
into the currently running program that invoked exec().

(Myself, I generally stay away from language building and extension 
features. I believe it leads to more complex languages - look at C++ - 
that can be difficult to follow.)

-- 
Bartc




More information about the Python-list mailing list