[Tutor] Re: List Comprehensions again [an example with filter()]
dman
dsh8290@rit.edu
Mon, 28 Jan 2002 09:25:46 -0500
On Sun, Jan 27, 2002 at 10:50:03PM -0500, Erik Price wrote:
| On Friday, January 25, 2002, at 10:19 PM, Danny Yoo wrote:
| Is using the three hashes a standard way of marking up example code in
| Python? I see it a lot on this list, and was wondering if it was
| specific to the language or just a convention of this list.
Danny's convention. Anything that is intuitively obvious works just
as well. (I often use a bunch of tilde's, though usually I'm quoting
some config file or shell commands on a different list)
| >###
| >def endsWithS(word):
| > return word[-1] == 's'
| >
| >for w in words:
| > if endsWithS(w):
| > print w
| >###
|
| This is something I haven't seen before -- using an expression as the
| return value of a function.
To be pedantic, you have seen an expression used in a return
statement. Everything that isn't a statement (ie 'print' or 'import')
is an expression. The only difference is that this expression is more
complicated than you've usually seen.
| I take it that this means "return boolean
| value TRUE if the expression 'word[-1] == "s" ' ". But I'm not sure.
| It could be that instead, it returns the words that match? If it did
| that, then the second part, the "for" loop, wouldn't be necessary, so
| drawing from context I am assuming that the former is true.
Yep.
| >You probably already feel comfortable passing around numbers and strings
| >to functions, but other functions? If this is the first time you've
| >seen
| >it, it will probably seem pretty darn weird at first, but it'll make
| >sense
| >as you play with it.
|
| You can create some pretty complicated constructs by passing functions
| to other functions in PHP -- I find that it's sometimes easier to
| conceptualize if I assign a function to a variable and then just pass
| variables to functions. But over time I'm sure I'll trade in this
| clarity for convenience.
Sometimes it is clearer to give the function a new name. It all
depends on the context -- what name does it already have and how do
you spell that name (ie is it a convoluted function call with complex
arguments or just a simple name?).
-D
--
If your company is not involved in something called "ISO 9000" you
probably have no idea what it is. If your company _is_ involved in ISO
9000 then you definitely have no idea what it is.
(Scott Adams - The Dilbert principle)