Emulating Pascal input

Grant Edwards grante at visi.com
Wed May 22 11:25:30 EDT 2002


In article <mailman.1022065417.1277.python-list at python.org>, Michael Williams wrote:

[...]

> This has the limitation that the user must pass the number of
> results they expect back. This is done implicitly in the Pascal
> version too (the number of variables passed to the function).
> However, error checking is difficult as this version stands.
> 
> If the student using the ninput function does, e.g.
> 
>>>> x = ninput(2)
> 5 6.0
> [5, 6.0] <type 'list'>

That looks pretty decent to me. What about it makes error
detection difficult?

> This is possibly what should be expected. However, passing the
> numerical argument representing the number of results expected
> does not strike me as a particularly graceful solution.

You could write a function that scans and converts an entire
line and returns however many numbers it finds (somebody
already posted something similar, so I'll leave the
implimentation as an exercise for the reader):

 x,y = readNumLine()
 
If readNumLine() returns something of length other than 2 (in
our example), you get an exception which you can trap.  Or you
can check the length explicitly:

 v = readNumLine()
 if len(v) != 2:
   bonk()
 x,y = v

> We would prefer something like the Pascal way, where the global
> variables in which the results are to be stored are passed as
> parameters. Can anyone suggest a way of doing this?

Python doesn't really work that way.  If you change it so it
does, then it's not Python any more.  :)

It's possibly to fake something by digging into Python's
internals and looking up the names of the parameters and
rebinding them in the global namespace, but that will break if
the parameters are not global. It will also earn the scorn of
the PSA underground, and that's too horrible to contemplate.

If you really want to do it the Pascal way, then you should
teach Pascal.  If you want to teach Python, then you're going
to be far better off in the long run learning to do things the
"Python way".

That said, from the "here's some more rope" department...

Another way to fake Pascals "var" parameters is to pass a
reference to a list, and modify that list:

>>> def foo(vec):
...   vec[0] = 1
...   vec[1] = 3
... 
>>> v = [9,8]
>>> foo(v)
>>> print v
[1, 3]
>>> 

IMO, this is also evil, though maybe less so than looking up
the names of parameters and changing their bindings in the
global namespace (which is what you describe).

> And if such an equivalent to the Pascal construct was
> implemented, what would be the chances of getting it included
> in, say, the string module of core Python.

It would be a fundamental change in the philosophy, structure,
and character of the language.  My first order estimate of the
chances for such a change being incorporated are 0.  But, I'm
not the one who decides such things...

> We would like such a function but would be reluctant to use it
> as we would then be teaching our students a ``dialect'' of
> Python.

-- 
Grant Edwards                   grante             Yow!  Am I accompanied by
                                  at               a PARENT or GUARDIAN?
                               visi.com            



More information about the Python-list mailing list