Re: Common LISP-style closures with Python
On 4.2.2012 4:47, Chris Rebert wrote: On Fri, Feb 3, 2012 at 4:27 PM, Antti J Ylikoski wrote: In Python textbooks that I have read, it is usually not mentioned that we can very easily program Common LISP-style closures with Python. It is done as follows: - # Make a Common LISP-like closure with Python. # # Antti J Ylikoski 02-03-2012. def f1(): n = 0 def f2(): nonlocal n n += 1 return n return f2 i. e. we can have several functions with private local states which are kept between function calls, in other words we can have Common LISP-like closures. Out of curiosity, what would be non-Common-Lisp-style closures? Cheers, Chris I understand that a "closure" is something which is typical of functional programming languages. -- Scheme-style closures, for example. I don't know Haskell, ML etc. but I do suspect that we could create closures in those languages as well. Maybe someone more expert than me can help? regards, Andy -- http://mail.python.org/mailman/listinfo/python-list
Re: Common LISP-style closures with Python
On 4.2.2012 12:14, Antti J Ylikoski wrote: On 4.2.2012 4:47, Chris Rebert wrote: On Fri, Feb 3, 2012 at 4:27 PM, Antti J Ylikoski wrote: In Python textbooks that I have read, it is usually not mentioned that we can very easily program Common LISP-style closures with Python. It is done as follows: - # Make a Common LISP-like closure with Python. # # Antti J Ylikoski 02-03-2012. def f1(): n = 0 def f2(): nonlocal n n += 1 return n return f2 i. e. we can have several functions with private local states which are kept between function calls, in other words we can have Common LISP-like closures. Out of curiosity, what would be non-Common-Lisp-style closures? Cheers, Chris I understand that a "closure" is something which is typical of functional programming languages. -- Scheme-style closures, for example. I don't know Haskell, ML etc. but I do suspect that we could create closures in those languages as well. Maybe someone more expert than me can help? regards, Andy This is how it is done in standard Common LISP: - ;;; Closure with Common LISP. ;;; ;;; Antti J Ylikoski 02-03-2012. (defun mak-1 () (let ((n 0)) #'(lambda () (incf n - kind regards, Andy -- http://mail.python.org/mailman/listinfo/python-list
Re: Common LISP-style closures with Python
On 4 February 2012 10:14, Antti J Ylikoski wrote: > On 4.2.2012 4:47, Chris Rebert wrote: >> Out of curiosity, what would be non-Common-Lisp-style closures? >> >> Cheers, >> Chris > > > I understand that a "closure" is something which is typical of functional > programming languages. -- Scheme-style closures, for example. > > I don't know Haskell, ML etc. but I do suspect that we could create closures > in those languages as well. Maybe someone more expert than me can help? I think what Chris asking is: what is the feature of Common-Lisp closures that Python closures share but other languages don't? I think what he is implying is that there is no such feature. Python closures are no more "Common-Lisp-style" than they are "Scheme-style" or "Smalltalk-like" or any other language-like. -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list
Re: Common LISP-style closures with Python
On 4.2.2012 12:58, Arnaud Delobelle wrote: On 4 February 2012 10:14, Antti J Ylikoski wrote: On 4.2.2012 4:47, Chris Rebert wrote: Out of curiosity, what would be non-Common-Lisp-style closures? Cheers, Chris I understand that a "closure" is something which is typical of functional programming languages. -- Scheme-style closures, for example. I don't know Haskell, ML etc. but I do suspect that we could create closures in those languages as well. Maybe someone more expert than me can help? I think what Chris asking is: what is the feature of Common-Lisp closures that Python closures share but other languages don't? I think what he is implying is that there is no such feature. Python closures are no more "Common-Lisp-style" than they are "Scheme-style" or "Smalltalk-like" or any other language-like. I would say that Python closures are equivalent with Common LISP closures (except that LAMBDA is more limited in Python, which is a feature which I don't like.) Do you maybe mean non-Common-LISP-style closures in Python? I cannot think of any ones. kind regards, Andy -- http://mail.python.org/mailman/listinfo/python-list
Re: Script randomly exits for seemingly no reason with strange traceback
On 2/3/2012 5:25 PM, Steven D'Aprano wrote: > Which version of Python, which version of Windows? I keep that information in my signature for every post I make to this list. CPython 3.2.2 | Windows NT 6.1.7601.17640 > If you upgrade Python, does the problem go away? I use the most recent stable version. It would be hard to say if the problem went away since it's rare and random AFAICT. On 2/3/2012 9:15 PM, Chris Angelico wrote: > Do you call on potentially-buggy external modules? It imports one module that does little more than define a few simple functions. There's certainly no (intentional) interpreter hackery at work. -- CPython 3.2.2 | Windows NT 6.1.7601.17640 -- http://mail.python.org/mailman/listinfo/python-list
Re: Script randomly exits for seemingly no reason with strange traceback
On Sat, 04 Feb 2012 10:32:25 -0600, Andrew Berg wrote: > On 2/3/2012 5:25 PM, Steven D'Aprano wrote: >> Which version of Python, which version of Windows? > I keep that information in my signature for every post I make to this > list. CPython 3.2.2 | Windows NT 6.1.7601.17640 Why so you do. Did you expect that people would read it? As a rule, sigs fade into the background -- my mail client colours it grey, my news client colours it light blue, and I generally don't even notice it. The Zen of Python applies here: explicit is better than implicit. >> If you upgrade Python, does the problem go away? > I use the most recent stable version. It would be hard to say if the > problem went away since it's rare and random AFAICT. I suggest you raise an issue on the bug tracker. If you can't reproduce the bug, it's unlikely to be fixed, but you might get lucky. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Common LISP-style closures with Python
On Sat, 4 Feb 2012, Antti J Ylikoski wrote: > On 4.2.2012 12:58, Arnaud Delobelle wrote: > > On 4 February 2012 10:14, Antti J Ylikoski wrote: > > > On 4.2.2012 4:47, Chris Rebert wrote: > > > > Out of curiosity, what would be non-Common-Lisp-style closures? > > > > > > > > Cheers, > > > > Chris > > > > > > > > > I understand that a "closure" is something which is typical of functional > > > programming languages. -- Scheme-style closures, for example. > > > > > > I don't know Haskell, ML etc. but I do suspect that we could create > > > closures > > > in those languages as well. Maybe someone more expert than me can help? > > > > I think what Chris asking is: what is the feature of Common-Lisp > > closures that Python closures share but other languages don't? > > > > I think what he is implying is that there is no such feature. Python > > closures are no more "Common-Lisp-style" than they are "Scheme-style" > > or "Smalltalk-like" or any other language-like. > > > > I would say that Python closures are equivalent with Common LISP closures > (except that LAMBDA is more limited in Python, which is a feature which I > don't like.) > > Do you maybe mean non-Common-LISP-style closures in Python? I cannot > think of any ones. > > kind regards, Andy AFAIK there is only one style for closure, similar to one style for square. There are quite a lot languages implementing closures, and quite a lot try to imitate them, including C with non-standard extension (without using those imitations I cannot say if they are good enough). http://en.wikipedia.org/wiki/Closure_(computer_science) Wrt lambdas, I really like blocks from Ruby (which AFAIK stem from blocks in Smalltalk, not sure if they call them "blocks"). http://lesscode.org/2005/07/12/ruby-colored-blocks-in-python/ http://railsguru.org/2010/03/learn-ruby-procs-blocks-lambda/ I mean, myself I am ok with lambdas (using them in languages where lambda is welcomed and contributing citizen) but blocks in place of lambdas would be nice to have in Python. Introduction of "with" construct was good IMHO, but if one likes coding style relying on passing anonymous pieces of code then Python might not be good choice for this. On the other hand, one can argue that using anonymous code too much is not the best style. I am not sure if extensive use of blocks/lambdas really helps, or if it contributes to "clever" hacks and a source of maintainance pain. So, perhaps it is good to have it in a few different ways - like, Ruby, Python and CL - and experiment with them all. In other words, rather than talking about making Python more like some other language(s) I think it is much better to learn those other language(s). If you'd like to try "unlimited" lambda, you might want to play with Racket, a Scheme superset. Or any other Scheme - it's simple enough to start coding after a day or two of learning (I mean Fibonaccis and Erastotenes sieves, not implementing database or web server). Myself, I would rather have blocks/lambdas and not need them rather than the other way, but that's just me. Regards, Tomasz Rola -- ** A C programmer asked whether computer had Buddha's nature. ** ** As the answer, master did "rm -rif" on the programmer's home** ** directory. And then the C programmer became enlightened... ** ** ** ** Tomasz Rola mailto:tomasz_r...@bigfoot.com ** -- http://mail.python.org/mailman/listinfo/python-list
Re: Script randomly exits for seemingly no reason with strange traceback
On 2/4/2012 11:06 AM, Steven D'Aprano wrote: > I suggest you raise an issue on the bug tracker. If you can't reproduce > the bug, it's unlikely to be fixed, but you might get lucky. Since I can't narrow it down to any specific circumstance or code, I'll gather information from a build of the interpreter with debugging enabled first. -- CPython 3.2.2 | Windows NT 6.1.7601.17640 -- http://mail.python.org/mailman/listinfo/python-list
Re: Script randomly exits for seemingly no reason with strange traceback
On Sun, Feb 5, 2012 at 3:32 AM, Andrew Berg wrote: > On 2/3/2012 9:15 PM, Chris Angelico wrote: >> Do you call on potentially-buggy external modules? > It imports one module that does little more than define a few simple > functions. There's certainly no (intentional) interpreter hackery at work. If it's safe for you to do so (copyright/licence etc), it may be worth posting the code along with your bug report, just in case. I had some REALLY weird issues from embedding Python that derived, ultimately, from buggy ref management - one such case came from forgetting to incref None; it took me a long time to track it down, because the problem didn't actually surface until the interpreter was shutting down. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: Common LISP-style closures with Python
On Sat, Feb 4, 2012 at 5:58 AM, Arnaud Delobelle wrote: > I think what Chris asking is: what is the feature of Common-Lisp > closures that Python closures share but other languages don't? > > I think what he is implying is that there is no such feature. Python > closures are no more "Common-Lisp-style" than they are "Scheme-style" > or "Smalltalk-like" or any other language-like. "No such feature"? What's that nonlocal thing then? The above function could not be written that way in Python 2. Of course maybe we want to put this feature in another category, but anyway, the function couldn't be written in some languages, even though they have closures. -- Devin -- http://mail.python.org/mailman/listinfo/python-list
Re: Common LISP-style closures with Python
On Sat, 04 Feb 2012 02:27:56 +0200 Antti J Ylikoski wrote: [...] > > # Make a Common LISP-like closure with Python. > # > # Antti J Ylikoski 02-03-2012. > > def f1(): > n = 0 > def f2(): > nonlocal n > n += 1 > return n > return f2 > [...] > > i. e. we can have several functions with private local states which > are kept between function calls, in other words we can have Common > LISP-like closures. > I'm not sure how naughty this is, but the same thing can be done without using nonlocal by storing the local state as an attribute of the enclosed function object: >>> def f(): ... def g(): ... g.count += 1 ... return g.count ... g.count = 0 ... return g ... >>> h = f() >>> j = f() >>> h() 1 >>> h() 2 >>> h() 3 >>> j() 1 >>> j() 2 >>> j() 3 This way, you can also write to the attribute: >>> j.count = 0 >>> j() 1 John -- http://mail.python.org/mailman/listinfo/python-list
Re: Common LISP-style closures with Python
On 5.2.2012 3:31, John O'Hagan wrote: On Sat, 04 Feb 2012 02:27:56 +0200 Antti J Ylikoski wrote: [...] # Make a Common LISP-like closure with Python. # # Antti J Ylikoski 02-03-2012. def f1(): n = 0 def f2(): nonlocal n n += 1 return n return f2 [...] i. e. we can have several functions with private local states which are kept between function calls, in other words we can have Common LISP-like closures. I'm not sure how naughty this is, but the same thing can be done without using nonlocal by storing the local state as an attribute of the enclosed function object: def f(): ... def g(): ... g.count += 1 ... return g.count ... g.count = 0 ... return g ... h = f() j = f() h() 1 h() 2 h() 3 j() 1 j() 2 j() 3 This way, you can also write to the attribute: j.count = 0 j() 1 John Yes, I do know that, but then it would not be a closure :-) Andy -- http://mail.python.org/mailman/listinfo/python-list