[Python-3000] Use cases for type annotations? (WAS: Type parameterization)

Steven Bethard steven.bethard at gmail.com
Sun May 21 01:56:08 CEST 2006


On 5/20/06, Collin Winter <collinw at gmail.com> wrote:
> On 5/20/06, Steven Bethard <steven.bethard at gmail.com> wrote:
> > I wandered around
> >     http://oakwinter.com/code/typecheck/
> > but couldn't find any discussion about the implications of, say,
> > requring the type ``{str:Number}`` (which I assume is basically
> > equivalent to the dict[str, Number] in this discussion).  Does that
> > mean that every access to the dict is checked?
>
> {str: Number} is indeed my syntax for dict[str, Number] (I didn't have
> the luxury of messing with the built-ins ; )
>
> http://oakwinter.com/code/typecheck/tutorial/builtintypes.html in
> particular discusses what it means to say that an argument should be
> {str: Number}: namely, that all keys must be strs and all values
> should be Numbers. It also talks about tuples, lists and sets.

So I should read a sentence like "This will assert that all the keys
in the dictionary are strs and all values are Numbers." as being
equivalent to dropping in the following code before the function is
called:

    for key, value in dictobj.items():
        assert isinstance(key, str)
        # okay, well not isinstance, but something like it
        assert isinstance(value, Number)

Is that right?

FWIW, it wasn't clear to me if "asserts" was literally an assert
statement, and if it was, it wasn't clear whether the assert
statements were placed directly before the function call, or if they
were delayed (e.g. via wrapping __getitem__ and __setitem__) until the
point at which they were actually necessary.

> I haven't done any real performance analysis on my system. My focus so
> far has been getting things working the way I want, saving the
> performance issue for later.

Ahh, ok.  Thanks.

STeVe
-- 
Grammar am for people who can't think for myself.
        --- Bucky Katt, Get Fuzzy


More information about the Python-3000 mailing list