[Types-sig] An ignorable wild idea

Martijn Faassen faassen@vet.uu.nl
Wed, 15 Mar 2000 19:21:28 +0100


Hi there,

I had another wild idea the other day that's probably fairly silly, so
of course I instantly thought I should share it with you all. If you
don't feel like having the discussion move out into a strange direction
simply don't reply or don't read this, though of course I wouldn't
post this if I didn't want feedback.

What are dynamic types in Python? They're basically attributes of Python
objects.

What would static types be? They're attributes of variables.

We change attributes of objects like this:

a.attribute = b

i.e., with the '.' notation and assignment. It's sometimes even possible
to change the type of an object (or at least the class, or the methods and
data a particular object has).

Now, the idea was introduce variable attributes. I'll use the operator
-> for that, but any operator would do.

a->attribute = b

This would change the attribute of the variable 'a' to whatever's in 'b'.

Since a type is an attribute of a variable, we'd set the type of a 
particular variable like this:

a->type = Integer

By default, all variables have the attribute 'type' set to 'Any'.

Variable attribute access is all analyzed and evaluated during compile
time, so one can't the result of some arbitrary Python expression into
a variable attribute at runtime. There needs to be a separate 
compile-time separate namespace for variable attribute assignments, with
some heavy restrictions. Accessing the variable attribute space during
runtime is no problem, though. This could work:

print a->type

and this too:

if a->type == Integer:
    # do whatever
    # though conditional type assignments should probably be disallowed:
    b->type = Integer

though it's doubtful how useful this would be.

Is this idea in fact useful at all, besides the nice parallel with object
attributes? I'm not sure. This might come in handy if you're doing
generic functions, perhaps:

a->type = foo->type # the variable a will contain the same type as foo.

And this variable attribute facility may have more uses. Perhaps it
could support docstrings:

a->doc = "Holds temporary value"

And it could be used for introspection, if any variable inside scope
is accessible through the -> operator:

def foo(a):
    b->doc = "holds a number"
    b = 5
    return b

print foo->a->type
print foo->b->doc

class Bar:
    def __init__(self):
        pass

    method->type = String
    def method(self):
        b->type = Integer
        b = 15
        return str(b)

print Bar->method->b->type

bar->type = Bar
bar = Bar()

if hasmethod(bar->type, 'method'):
    bar->method()

Where 'hasmethod' could be evaluated at compile-time, so we'd get this:

if 1:
   bar->method()

but this if we removed 'method':

if 0:
   bar->method()

Of course you'd need many restrictions about what can be done with
a class and objects at run time, but you need those in any case if you
do static type checking.

Anyway, these are mostly idle speculations, based on the idea that variables
themselves have attributes. Just wanted to let you all know, if this is news
at all. Many questions are left unanswered. :)

Regards,

Martijn