Movatterモバイル変換
[0]ホーム
Named code blockes
James_Althoff at i2.comJames_Althoff at i2.com
Mon Apr 23 14:55:44 EDT 2001
Alex Martelli wrote:Is it so 'costly' to give these statement suites a name?<jima>I think it is. In Smalltalk, for example, you can writesomething like (approx.):collection do: [:item | item doThis. item doThat]This passes an arbitrary and unnamed block of code tothe collection object for executing.in Python this would be:def doThisAndDoThatToItem(item): item.doThis() item.doThat()collection.do(doThisAndDoThatToItem)Now suppose the call to collection.do(xxx) is in the middle of avery long class def. Then I have to put the doThisAndDoThatToItemdef before the class or after the class thereby separating the definitionfrom its one and only use by possibly hundreds of lines ofcode. This is not nice for readability. Or I have to definedoThisAndDoThatToItemas a method in my class which clutters my class with an unnecessarymethod. Or I have to resort to extracting the items of thecollection for use in a for loop (for example) which means the collectionmust be written in such a way as to expose its items as a sequence(which the collection writer might not have wanted to do).Unnamed blocks are a very powerful programming device. As far as I cantell, the main reason that Python does not support them is that no onehas figured out how to handle the indentation issues -- the syntax thing ;-)</jima>I generally find that turning a lambda into a (named) localfunction makes my code clearer in all but the most trivialcases -- if that applies to expressions, it should a fortioriapply to suites.> (imo, nested scopes are pretty useless without real blocks)I'm not so hot about nested scopes, but, anyway, we DO have'real' blocks -- surely, giving them a name doesn't taketheir reality away, does it?-)
More information about the Python-listmailing list
[8]ページ先頭