Steven D'Aprano wote: > But if you really want declarations, you can have them. > > >>> import variables > >>> variables.declare(x=1, y=2.5, z=[1, 2, 4]) > >>> variables.x = None > >>> variables.w = 0 > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > File "variables.py", line 15, in __setattr__ > raise self.DeclarationError("Variable '%s' not declared" % name) > variables.DeclarationError: Variable 'w' not declared
Another way is to decorate functions with their local variables: >>> from strict import my >>> @my("item") ... def f(x=1, y=2.5, z=[1,2,4]): ... x = float(x) ... w = float(y) ... return [item+x-y for item in z] ... Traceback (most recent call last): File "<stdin>", line 2, in <module> File "strict.py", line 11, in dec raise DeclarationError("No slot for %s"%varname) strict.DeclarationError: No slot for w and the implementation import re class DeclarationError(TypeError): pass def my(slots=""): tokens = slots.split() def dec(func): code = func.func_code for varname in code.co_varnames[code.co_argcount:]: if re.match('\w+$', varname) and varname not in tokens: raise DeclarationError("No slot for %s"%varname) return func return dec The best way to catch false rebindings is to stick a comment with the word "rebound" after every statement where you think you're rebinding a variable. Then you can search your code for cases where there's a "rebound" comment but no rebinding. Assuming you're the kind of person who knows that false rebindings can lead to perplexing bugs, but doesn't check apparent rebindings in a paranoid way every time a perplexing bug comes up, anyway. (They aren't that common in modern python code, after all.) And that you remembered to add the comments (like you would have remembered the let and set). And you're also the kind of person who's troubled by perplexing bugs but doesn't run a fully fledged lint. Maybe that's the kind of person who wouldn't put up with anything short of a macro as in the original proposal. All I know is that it's the kind of person I don't want to second guess. Graham -- http://mail.python.org/mailman/listinfo/python-list