- READABILITY.
Python's easier to write and read. It has a simple statement-based,
procedural syntax. Here's an example which intersects 2 sequences:
def intersect(item1, item2):
result = []
for x in item1:
if x in item2: result.append(x)
return result
Here, item1 and item2 can be any sequence data type (lists,
strings, user-defined classes, C extension types,..).
- SEMANTIC POWER.
Python's also usually considered more powerful than TCL, yet
applicable to the same problem domains -- extension-language
work, shell scripts, prototyping, etc. It's a step-up from TCL
as a language, but still has great interfaces to/from C (dynamic
loading, C modules act just like Python ones,..).
Python's an object-oriented language, and has powerful built-in
data and program structuring tools -- lists, dictionaries, modules,
classes, exceptions,... For instance, if 'intersect' above were a
class's method, we could overload the '&' operator to run it; we
could also speed it up by using dictionaries, make it more generic...
Here's a more relevant example: a simple Tk program in Python
which puts up a window with a single button [this could be more
compact]:
from Tkinter import * # get the Tk module
class Hello(Frame): # subclass our app
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.make_widgets()
def make_widgets(self):
self.hello = Button(self, {'text': 'Hello GUI world!',
'command' : self.quit})
self.hello.pack({'side': 'left'})
Hello().mainloop()
- TRY IT, YOU'LL LIKE IT...
Have you ever tried Python? For instance, indentation syntax
turns out to be pretty handy in a rapid-development language, and
is an implicit quality-control feature (whatever that means :-).
Because of its features and readability, Python can be better
than TCL for non-trivial tasks. Like all dynamic languages, Python
may not be the best tool for delivery of large, static components.
But it's great for 'front-end' type tasks in mixed/hybrid systems,
as a prototyping tool, etc. For example, I'm currently using it as
an extension language for on-site customization of C++ binaries.
But examples beat words: I've tacked-on another example below, from
a module that redirects standard input/output streams to instances of
classes that simulate files; probably not the best example, but enough
to illustrate some distinctions between TCL and Python.
Again, YMMV on all of this. And to be fair, Python's borrowed the Tk
tool-kit, a TCL-land invention. But I'd encourage you to have a look
at Python before closing-the-book.
Mark Lutz
lutz@kapre.com
------------------------------------------------------------------------
# file (module) redirect.py
# part of an example that redirects i/o streams to objects
import sys # load a module (python or C)
class FakeStream: # a class def
def close(self): # its methods
pass
def flush(self):
pass
def isatty(self):
return 0
class Input(FakeStream):
def __init__(self, input): # plus readline,...
self.text = input
def read(self, *size):
if not size:
result, self.text = self.text, ''
else:
bytes = size[0]
result, self.text = self.text[:bytes], self.text[bytes:]
return result
class Output(FakeStream):
def __init__(self):
self.text = ''
def write(self, string):
self.text = self.text + string
def writelines(self, lines):
for line in lines: self.write(line)
def testfunc(input, function, args): # a function def
save_stdio = sys.stdin, sys.stdout
sys.stdin = Input(input) # make instances
sys.stdout = Output()
try: # catch errors here
apply(function, args) # run the function
except:
sys.stderr.write('error during function!\n')
result = sys.stdout.text
sys.stdin, sys.stdout = save_stdio
return result # output as a string
------------------------------------------------------------------------