Why I chose Python over Ruby

Schüle Daniel uval at rz.uni-karlsruhe.de
Sun Mar 5 19:34:41 EST 2006


Hi Alex

[...]

> The trick about distinguishing a name's exact nature based on whether
> the compiler sees an assignment to that name in some part of code is
> found in both languages, albeit in different ways. In Ruby, as you've
> pointed out, it's the heuristic used to disambiguate local variable
> access from zero-argument method calls, and the "part of code" is the
> function up to the point of access. In Python, it's used to disambiguate
> local from global or free variables, and the "part of code" is the body
> of the whole function (Ruby does not need to make this latter
> distinction because it strops global names with a leading sigil -- $a is
> always the global variable a, just like @a is always the instance
> attribute a, which we'd write self.a in Python). Another subtle case in
> Ruby is whether an assignment such as a=23 _within a block_ is meant to
> be local to the block or meant to rebind local name 'a' within the
> enclosing function; again, the heuristic is similar, depending only on
> whether the compiler had seen another assignment to a before it saw the
> block (Python forbids the rebinding of variables coming from an
> enclosing but non-global scope, to avoid facing this issue).

I am not sure what you mean here
can you elaborate on this please

 >>> def a():
...     q = []
...     def b(x):
...             def c(y):
...                     def d(z):
...                             q.append(x)
...                             q.append(y)
...                             q.append(z)
...                     d(1)
...             c(2)
...     b(3)
...     return q
...
 >>> a()
[3, 2, 1]

As far as I know this snippet would work only from version 2.2
maybe you are talking about older versions of Python

Regards, Daniel




More information about the Python-list mailing list