Re: Python?

Steven D. Majewski (sdm7g@elvis.med.Virginia.EDU)
Tue, 29 Mar 1994 18:49:00 GMT

In article <2n9ahu$gst@twonky.btv.ibm.com>,
Dale Pontius <pontius@btvlabvm.vnet.ibm.com> wrote:
>
>I would say that a preliminary faq is in order here, [ ... ]
>
>With no facts, I'd say Python is a Lisp dialect using COBOL
>syntax style.
>

Wrong on both counts, Dale - but pretty good, for no facts! :-)

Python is not related to ( or very similar to ) Lisp.

Guido van Rossum, the creator or Python, believes in a firm distinction
between functions and procedures, and between expressions and
statements, which ( I have complained at times )
makes it difficult to program in a consistantly functional STYLE
in Python. ( He says my mind has been permanently warped by
programming in C for too long, and that I just resist programming
in Python style! :-)

Python does have high-level data types like lists, tuples,
dictionaries, and strings.

Lists, for example, have methods:
['append', 'count', 'index', 'insert', 'remove', 'reverse', 'sort']

They respond to the standard (generic-sequence) funcion len(),
they can be indexed ( list[i] ) or sliced( index[i:j] ),
and they can be appended with the "+" operator or iterated thru
with 'for elem in list:' ( as can other sequence types ).

but:
list = [1,2,3]

"len( list )" returns the value 3.
The expression: "list + list[1:]" returns the list [1, 2, 3, 2, 3]

But "list.append( 4 )" does not return a value, it applies the method
append to the object list, leaving list with the value [1, 2, 3, 4]
as a side effect.

[ Actually, the above DOES return a value - it returns the value
'None' - which is not the same as an empty list [] or tuple (),
although all of them, and the empty string '' and the value zero
are considered false in a Boolean context. The python interpreter
typically prints the value of all expressions, unless the value is
None. Unlike 'C', assignment does not have a value as an expression,
so, typing "f(x)" will print the value returned by f(x), but typing
"y = f(x)" will not print. ]

In Python, assignment with "=" is usually a binding of a name to a value,
except in the case of mutable objects ( like lists and dictionaries )
where assignment of a element is a modification of that object.

And there are some lisp like functions like apply, map, filter, reduce,
etc. ( There is a lambda, but a recent and current topic of discussion
on the python-list mailing list has been the fact that since Python
does not have arbitrary levels of nested scope like Lisp, the binding
of free arguments in lambda doesn't always do what a Lisp-er would
expect. We have been discussing syntax changes to lambda to handle
this. )

Sometimes you will see, instead of ( or in addition to ) functional
composition, a sort of function/object composition like:

for line in posix.popen( 'finger', 'r' ).readlines():
print line[:-1]

Where the method 'readlines()' is being applied to the (anonymous)
object returned by the function 'popen()' in module 'posix'

As to COBOL syntax - I don't see much in common, other than the use
of "." as a connector, and in python it is more like C/C++ ( or
more exactly like Modula-* )

Python has a distinctive indent/dedent syntax, where indentation level
is used to indicate blocks. There is no begin/end, {/}, tokens.

user = sys.argv[1]
root = 'root'
for line in posix.popen( 'ps aux', 'r' ).readlines() :
if line[:len(user)] == user :
print ">>>", line[:-1], "<<<"
elif line[:len(root)] == root :
pass
else:
print " ", line[:-1]

Some folks find this unusual, but most people read the indenting
anyway - this way it is not possible to be mislead by the formatting.

[ You can stuff things onto a single line:

if cond : do_one; do_two; do_three

except that you can't do something ambiguous like:

for x in lista : if x in listb : print x

]

- Steve Majewski (804-982-0831) <sdm7g@Virginia.EDU>
- UVA Department of Molecular Physiology and Biological Physics