From: Ville Vainio Date: 2004-04-06T15:34:20+09:00 Subject: Re: deciding between ruby and python >>>>> "Gavin" == Gavin Sinclair writes: >> Python. And if the needs of your anonymous function exceed what lambda >> provides, you can use what I call big L notation: >> def L(matchobj): >> return matchobj.group(0).upper() >> res = re.sub('u|a', L, 'huijaahuijaa') Gavin> That's a poor man's substitute. Seriously. It means you have to name Gavin> everything you're doing, and pollute the function namespace *for no Gavin> good reason*. Yes, you can "do it", but it's ugly in comparison. You don't really pollute it much, because you can always use the same name. The old name (and as such, the old definition too) is clobbered by the new name. If you always use big L for this purpose, Also, you can do the deffing inside functions too. So it's not even visible at module level. Gavin> Can you provide an implementation in the same style in Python? It's Gavin> not that I don't believe Python can count characters, lines and words Gavin> in a string. But I don't believe it can carry outside state into its Gavin> "substitute for blocks". Yes. Python supports lexical closures. The only gotcha is that you can't 'assign' the names of free variables to point to new objects, i.e. it has to be the same object, though it can be mutated. So you can use e.g. length 1 array to contain the other object. The following code: ------------------ def get_char_counter(): chars = [0] def filepusher(f): chars[0] += len(open(f).read()) return chars[0] return filepusher acc = get_char_counter() print acc("/bootsect.dos") print acc("/bootsect.dos") ------------------- Prints out: ------------------- 201 402 ------------------- Gavin> # Connection: the database connection is automatically released at Gavin> # the end of the block, including if there is an exception therein. try - finally blocks are used for this in Python. Gavin> # Transaction support: any exception in this block will cause a Gavin> # "ROLLBACK". If all is fine, a "COMMIT" is performed at the end. try: stuff() except: rollback() else: commit() But I admit that the ruby approach seems cleaner here. It's probably easier to use and the logic doesn't clutter the code. Gavin> I'd like to see the same program written in Python. I don't know the Gavin> Python database interface, but I expect it would need to have explicit Gavin> exception handling. There would also be no automatic releasing of Gavin> resources. Back when I did db programming, I didn't think it was such a big deal. It seems bigger on the paper, though. Gavin> To prove me wrong, please present your best effort at translating the Gavin> database programming example above into Python. Of course, you may Gavin> disagree with my frame of reference instead, but I think that's pretty Gavin> good. Yes, I do indeed disagree with your frame of reference. I just don't think this issue such big a deal. you might want to go googling for more elaborate discussion - code blocks have indeed been suggested for Python, they just didn't happen. It seems to pop up every now and then, so perhaps they will be in Python some day if they are deemed valuable enough. In the meantime, I don't care much - Try-finally is good enough for me :-). I would go googling myself, but I don't really have the time. There's work to do, and I'm 'between ADSL operators' at home for this month. -- Ville Vainio http://tinyurl.com/2prnb