Skip to content

Python's variable model is weird #2

@arnsholt

Description

@arnsholt

Python's variable model is going to be at least a moderate pain to get exactly right. The problem is, of course, that variables aren't declared explicitly, but implicitly by assigning to them. This in itself isn't much of a problem to handle in NQP, but it's compounded by the fact that reading from an unbound variable should throw an exception.

Thus, the validity of the following snippet cannot be statically determined:

for x in things: pass
print(x)

It can either print the last element of the list, or result in an exception if the list is empty (because x will not have been assigned to). Similar things can be achieved by assigning to variables in a while loop, or only some of the branches of an if.

The easy way to solve this would be for every block to have a hash that contains all the variables (which is probably the implementation strategy that gave rise to this stuff in the first place), but that's likely going to be kind of slow, so I'd like to avoid that. In the end, it may be easiest to implement this as a custom nqp::op (or rather two, one for reading and one for binding) that does the necessary magic.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions