What's the best way to minimize the need of run time checks?
BartC
bc at freeuk.com
Sun Aug 14 06:31:10 EDT 2016
On 14/08/2016 05:13, Steven D'Aprano wrote:
> On Sun, 14 Aug 2016 06:59 am, BartC wrote:
>> * The fields in my record are known at compile time
> There's nothing like that in Python. But the difference between compile-time
> and run-time is not that significant in practice (as opposed to theory,
> where it is critical).
But this is, sort of, the subject of the thread. I gave record fields as
one area where they could be made static by pre-declaring, without too
many objections.
>
>> * (My record fields have other features: aliases for any field; the
>> ability to access by index, doing len() etc; fast conversions to and
>> from a list; also the ability to create packed records that exactly
>> match C structs, and so on.)
>
> I'm not sure what you mean by aliases, or how you would use them. Got an
> example?
(
record date =
var day, month, year
var giorno @day
end
So 'giorno' is a synonym for 'day'. Or sometimes a field is used for
different purposes in different contexts, and a more apt name might be
useful:
var text
var window_caption @ text
var button_text @ text
(This can be done also with the more formal 'union' syntax, but I only
allow that for packed structs. It might look like this within a record def:
union
var text, window_caption, button_text
end
))
>> If I use the Python record() to create two records R and S, then
>> type(R) and type(S) seem to give the same result (both 'type').
>
> Of course: the type of a class is "type":
>
> py> type(str), type(int), type(list), type(type)
> (<class 'type'>, <class 'type'>, <class 'type'>, <class 'type'>)
>
>
> This is called the metaclass; the class of a class. And yes, type is its own
> type. What you should be doing is comparing the types of record *instances*
> instead:
> (<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:
Actually I thought I was comparing instances, but I was applying type()
to the return value of record(). But doing that now, I get the above too.
I get this behaviour with my scheme:
d := new(date) # d is an instance of a date record
println date # <date> is displayed
println date.type # <type>
println d.type # <date>
println d.basetype # <record>
--
Bartc
More information about the Python-list
mailing list