开发者

Anonymous code blocks

Preface:

So far I've used Python as a full scale programming language. Now I like to use it to write down some notes (comments) with some calculation (Python code) here and there (I'm actually using Emacs and send the current buffer again and again to a running IPython instance).

The problem: I like to reuse some common variable names like 'A' or 'd' multiple times within the same document without running into the problem that I accidentally forget to reassign the value to one of these variable names.

I so far abuse the class s开发者_开发知识库tatement

# Topic one: bla bla
class _anon:
    d = 10
    A = d**2 * pi /4

# Topic two: bla bla
class _anon:
    A = d**2 * pi /4 # error is raised since d is missing

This works, since a class statement creates an execution frame which works as a variable scope, but I wonder if there is dedicated syntax for this use case.


If I want to simulate C/C++ blocks, this usually does the trick:

def _anon():
    d = 10
    A = d**2 * pi /4
_anon()

These "blocks" are able to be nested.


If a small piece of initial boilerplate is acceptable, you can also use a decorator to call the function:

def block(function):
    function()

Now you don't have to repeat its name:

@block
def _anon():
    d = 10
    A = d**2 * pi /4

Note that function decorators usually return a function, but this one doesn't. The result is that the name "_anon" points to "None" after executing the code, so the code block really is anonymous.


No. Only class and def blocks and modules (and in more recent versions, list comprehensions and generator expressions - not appliable here though) introduce a new scope. And only classes execute directly. So if you want to continue this debatable use of Python, you'll have to stick with abusing class, or define functions and call them directly. Using a different file for each calculation is slightly less ugly at source code level, but propably not worth it if the calculations are always that small.


You could always explicitly delete your values after each block, with del:

d = 10
A = d**2 * pi /4
(...)
del d, A

# Topic two: bla bla

A = d**2 * pi /4 # error is raised since d is missing

"Explicit is better than implicit." "Readability counts." "There should be one-- and preferably only one --obvious way to do it."

If you want fancy stuff, it is possible to make unit tests that fail if a "one letter variable" is alive "too far from the point it was created", writing tests that enable debugging trace to verify for that. In this way you would not forget adding the del statements.


globals.clear() almost works:

>>> a = 5
>>> b = 6
>>> a
5
>>> b
6
>>> globals().clear()
>>> a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
>>> b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'b' is not defined

The problem is that it will blow away everything including useful stuff like globals itself! If you try to do globals.clear() a second time, you'll see:

>>> globals.clear()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'globals' is not defined

For a solution to this, see http://bytes.com/topic/python/answers/34902-how-clean-python-interpreters-environment#post129239

But if I were going to this much trouble to bend Python into being a calculation engine, I might wonder if there's a better tools for the purpose like GNU Octave.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜