From: Paul Prescod Date: 2003-03-21T07:08:06+09:00 Subject: Re: Strong advantages over Python Greg McIntyre wrote: >... > My answers currently include 'meta' programming (trapping undefined > method calls, defining new class/module methods/attributes dynamically, > etc.), refactoring control flow (Python has generators but they're more > clumsy to use), invisible resource management (e.g. File#open { ... > }), Array and Hash being extensible classes (Python has UserList and > UserDictionary, but they're annoying and AFAIK do not work well with > the existing control structures which expect primitive lists and > dictionaries). Please verify these publically before you teach this stuff to these students. I'm not going to get into an argument about whether Ruby is better than Python, but I do know for a fact that Python can do some things that you say it cannot. There may be nuances that are unique to Ruby but the overall features are there. For instance: ~/p4/usr/pprescod> cat foo.py class Foo: def __getattr__(self, attrname): return lambda : attrname f = Foo() print f.a() print f.b() print f.c() Foo.special = lambda(self) : "In dynamically assigned method" f.special() f.special2 = lambda : "In per-instance dynamically assigned method" print f.special2() for i in range(0, 10000): file = open("/tmp/test.txt") class MyDict(dict): def foo(self): return "foo" class MyList(list): def bar(self): return "bar" d = MyDict() d["a"] = 5 d[5] = "a" print d.foo() for i in d: print i, d[i] m = MyList([1,2,3,4]) m[0]= "a" print m.bar() for i in m: print i class MyInt(int): def doSomething(self): return "Did something" x = MyInt(5) print x.doSomething() ~/p4/usr/pprescod> python foo.py a b c In per-instance dynamically assigned method foo a 5 5 a bar a 2 3 4 Did something