[Python-Dev] Pre-PEP: Allow Empty Subscript List Without Parentheses
Talin
talin at acm.org
Sun Jun 18 01:30:45 CEST 2006
Martin v. Löwis wrote:
> Noam Raphael wrote:
>
>>I meant the extra code for writing a special class to handle scalars,
>>if I decide that the "x[()]" syntax is too ugly or too hard to type,
>>so I write a special class which will allow the syntax "x.value".
>
>
> What I cannot understand is why you use a zero-dimensional array to
> represent a scalar. Scalars are directly supported in Python:
>
> x = 5
>
> Also, in an assignment, what are you putting on the right-hand side?
> A read access from another zero-dimensional array?
Ok, so in order to clear up the confusion here, I am going to take a
moment to try and explain Noam's proposal in clearer language.
Note that I have no opinions about the merits of the proposal itself;
However, the lack of understanding here bothers me :)
The motivation, as I understand it, is one of mathematical consistency.
Let's take a moment and think about arrays in terms of geometry. We all
learned in school that 3 dimensions defines a volume, 2 dimensions
defines a plane, 1 dimension defines a line, and 0 dimensions defines a
point.
Moreover, each N-dimensional entity can be converted to one of lower
order by setting one of its dimensions to 0. So a volume with one
dimension set to zero becomes a plane, and so on.
Now think about this with respect to arrays. A 3-dimensional array can
be converted into a 2-dimensional array by setting one of its dimensions
to 1. So a 5 x 5 array is equivalent to a 5 x 5 x 1 array.
Similarly, a 3-dimensional array can be converted into a 1-dimensional
array by setting two of its dimensions to 1: So an array of length 5 is
equivalent to a 5 x 1 x 1 array.
We see, then, a general rule that a N-dimensional array can be reduced
to M dimensions by setting (N-M) of its dimensions to 1.
So by this rule, if we reduce a 3d array to zero dimensions, we would
have an array that has one element: 1 x 1 x 1.
Similarly, each time we reduce the dimension by 1, we also reduce the
number of indices needed to access the elements of the array. So a 3-d
array requires 3 coordinates, a 2-d array requires 2 coordinates, and so on.
It should be noted that this zero-dimensional array is not exactly a
normal scalar. It is a scalar in the sense that it has no dimensions,
but it is still an array in the sense that it is contains a value which
is distinct from the array itself. The zero-dimensional array is still a
container of other values, however it can only hold one value. This is
different from a normal scalar, which is simply a value, and not a
container.
Now, as to the specifics of Noam's problem: Apparently what he is trying
to do is what many other people have done, which is to use Python as a
base for some other high-level language, building on top of Python
syntax and using the various operator overloads to define the semantics
of the language.
However, what he's discovering is that there are cases where his
syntactical requirements and the syntactical rules of Python don't match.
Now, typically when this occurs, the person who is creating the language
knows that there is a rationale for why that particular syntax makes
sense in their language. What they often do in this case is to try and
convince the Python community that this rationale also applies to Python
in addition to their own made-up language. This is especially the case
when the proposed change gives meaning to what would formerly have been
an error. (I sometime suspect that the guiding design principle of Perl
is that all possible permutations of ASCII input characters should
eventually be assigned some syntactically valid meaning.)
Historically, I can say that such efforts are almost always rebuffed -
while Python may be good at being a base for other languages, this is
not one of the primary design goals of the language as I understand it.
My advice to people in this situation is to consider that perhaps some
level of translation between their syntax and Python syntax may be in
order. It would not be hard for the interactive interpreter to convert
instances of [] into [()], for example.
-- Talin
More information about the Python-Dev
mailing list