[Python-Dev] Pre-PEP: Allow Empty Subscript List Without Parentheses
shane at hathawaymix.org
Sun Jun 18 09:54:36 CEST 2006
Noam Raphael wrote:
> 2006/6/17, "Martin v. Löwis" <martin at v.loewis.de>:
>> 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
> I need a zero-dimensional array as a single cell - an object that
> holds a value that can change over time. It works just like a cell in
> a spreadsheet: For example, say that if you change the value of cell
> A1 to 0.18, cell A2 changes to 5. When using the library I design, you
> would write "sheet1[0, 0] = 0.18", and, magically, "sheet1[0, 1]" will
> become 5. But in my library, everything is meaningful and doesn't have
> to be two-dimensional. So, if in the spreadsheet example, A1 meant the
> income tax rate, you would write "income_tax = 0.18", and,
> magically, "profit['Jerusalem', 2005]" will become 5.
Try to think more about how users will use your API. You haven't
specified where those names (sheet1, income_tax, and profit) are coming
from. What do you expect users of your library to do to bring those
names into their namespace?
Let me take a wild guess so you can see what I'm asking:
sheet1 = spreadsheetlib.sheet('sheet1')
income_tax = spreadsheetlib.cell('income_tax')
profit = spreadsheetlib.cell('profit')
So far, that's a mess! What are you really going to do? Will it be
better? This could be a much greater concern than optimizing away
A possible way to solve the namespace problem is to make all names an
attribute of some object.
from spreadsheetlib import sp
sp.sheet1[0, 0] = 0.18
assert sp.sheet1[0, 1] == 5
sp.income_tax = 0.18
assert sp.profit['Jerusalem', 2005] == 5
That would be a pretty usable API, IMHO, and you'd be able to write it
now without any changes to Python.
More information about the Python-Dev