Multiline lamba implementation in python.
I don't expect multiline lambdas to be added to Python. I'm not so sure that
that's a bad thing. Regardless, isn't it possible to write your own
implementation of multiline lambdas as functions? Wouldn't that be a win-win
for everyone?
Anyway, the meat of the idea is this:
def myLambda(args, body):
fun = 'def f(' + args + '):' + body
exec fun
return f
... ... ... ...
funWithNoName = myLambda('x', '''
print x
return(x**2)''')
... ... >>>
[myLambda('x', '''\n print x\n return(x**2)''')(x) for x in range(3)]
0
1
2
[0, 1, 4]
There are a few tricks with the indenting, no question. myLambda should be
more intelligent than it is, I'm thinking of using regular expressions to do
simple indentation checking. Lexical variable scoping would be tricky, but
that's not one of a python's strong suits.
What am I missing?
Josh.
--
http://mail.python.org/mailman/listinfo/python-list
Re: Multiline lamba implementation in python.
On 6/12/07, Facundo Batista <[EMAIL PROTECTED]> wrote: Josh Gilbert wrote: > I don't expect multiline lambdas to be added to Python. I'm not so sure that > that's a bad thing. Regardless, isn't it possible to write your own Yes, it is a bad thing. Why? Because it would another way to do something you can do in other way. The *only* big value of lambda is than you can code a function in a small place, like: bt = Button(...,...,..., lambda evt: None) If you want to grow that in multiline, just use a named function: def multilinefunct(...): ... Remember that the *only* difference between the two functions is that one is anonymous, and for other you have to came up with a name (name that if is well thought, actually adds readibility to your code). Regards, -- . Facundo . Blog: http://www.taniquetil.com.ar/plog/ PyAr: http://www.python.org/ar/ -- http://mail.python.org/mailman/listinfo/python-list Guido doesn't want multiline lambdas, that's a really good reason not to add them. They are, on the other hand, very useful at times. Even if he did add multiline lambdas it's not certain that he would allow statements in them, currently they're expression only. The beauty of my suggestion is that you can define an anonymous multiline functions with statements in them. I know that the standard Python response is that you might as well define a function, indeed, the name might provide useful documentation. In reality, however, the vast majority of my anonymous functions are callbacks (tends to lead to names like mouseUp_callback) and functions passed into list comprehensions. I don't want to have to name them, it breaks the flow. My technique allows for anonymous functions of arbitrary complexity which is what I really want. I really don't think that I'm alone here, the lack of a multiline lambda has been bemoaned for years (recently here http://www.dehora.net/journal/2007/06/payne.html). -- http://mail.python.org/mailman/listinfo/python-list
Re: Questions about mathematical and statistical functionality in Python
On Thursday 14 June 2007 5:54 pm, Tim Churches wrote: > Michael Hoffman wrote: > > Talbot Katz wrote: > >> I hope you'll indulge an ignorant outsider. I work at a financial > >> software firm, and the tool I currently use for my research is R, a > >> software environment for statistical computing and graphics. R is > >> designed with matrix manipulation in mind, and it's very easy to do > >> regression and time series modeling, and to plot the results and test > >> hypotheses. The kinds of functionality we rely on the most are standard > >> and robust versions of regression and principal component / factor > >> analysis, bayesian methods such as Gibbs sampling and shrinkage, and > >> optimization by linear, quadratic, newtonian / nonlinear, and genetic > >> programming; frequently used graphics include QQ plots and histograms. > >> In R, these procedures are all available as functions (some of them are > >> in auxiliary libraries that don't come with the standard distribution, > >> but are easily downloaded from a central repository). > > > > I use both R and Python for my work. I think R is probably better for > > most of the stuff you are mentioning. I do any sort of heavy > > lifting--database queries/tabulation/aggregation in Python and load the > > resulting data frames into R for analysis and graphics. > > I would second that. It is not either/or. Use Python, including Numpy > and matplotlib and packages from SciPy, for some things, and R for > others. And you can even embed R in Python using RPy - see > http://rpy.sourceforge.net/ > > We use the combination of Python, Numpy (actually, the older Numeric > Python package, but soon to be converted to Numpy), RPy and R in our > NetEpi Analysis project - exploratory epidemiological analysis of large > data sets - see http://sourceforge.net/projects/netepi - and it is a > good combination - Python for the Web interface, data manipulation and > data heavy-lifting, and for some of the more elementary statistics, and > R for more involved statistical analysis and graphics (with teh option > of using matplotlib or other Python-based graphics packages for some > tasks if we wish). The main thing to remember, though, is that indexing > is zero-based in Python and 1-based in R... > > Tim C Thirded. I use R, Python, Matlab along with other languages (I hate pipeline pilot) in my work and from what I've seen nothing can compare with R when it comes to stats. I love R, from its brilliant CRAN system (PyPI needs serious work to be considered in the same class as CPAN et al) to its delicious Emacs integration. I just wish there was a way to distribute R packages without requiring the user to separately install R. In a similar vein, I wish there was a reasonable Free Software equivalent to Spotfire. The closest I've found (and they're nowhere near as good) are Orange (http://www.ailab.si/orange) and WEKA (http://www.cs.waikato.ac.nz/ml/weka/). Orange is written in Python, but its tied to QT 2.x as the 3.x series was not available on Windows under the GPL. Josh Gilbert -- http://mail.python.org/mailman/listinfo/python-list
Re: Multiline lamba implementation in python.
On Wednesday 13 June 2007 4:04 am, Duncan Booth wrote:
> "Gabriel Genellina" <[EMAIL PROTECTED]> wrote:
> > But you already have "multiline" lambdas right now in that sense, no
> > need to add anything. I think you were talking about lambdas *with*
> > statements inside.
> >
> > bin = lambda x:((x&8 and '*' or '_') +
> > (x&4 and '*' or '_') +
> > (x&2 and '*' or '_') +
> > (x&1 and '*' or '_'))
>
> Or in more recent versions of Python:
>
> bin = lambda x:(('*' if x&8 else '_') +
> ('*' if x&4 else '_') +
> ('*' if x&2 else '_') +
> ('*' if x&1 else '_'))
>
> but seriously, any example of lambda which simply assigns the function to a
> variable is flawed.
>
> I can sort of understand the people who object to a named function taking
> the logic 'out of line', but any expression which actually requires a
> multi-statement function to be embedded in the middle of it is already in
> danger of causing my brain to implode.
You're correct, my lambda function handles statements as well as multiple
expressions. That code, however, is bloody hideous. If that's what I had to
do to do multiline callbacks I'd always use def. Ew.
--
http://mail.python.org/mailman/listinfo/python-list
