Why is it different about '\s' Matches whitespace and Equivalent to [\t\n\r\f]?
Hi, On a tutorial it says that '\s': Matches whitespace. Equivalent to [\t\n\r\f]. I test it with: re.match(r'\s*\d\d*$', ' 111') _sre.SRE_Match object at 0x03642BB8 re.match(r'\t\n\r\f*\d\d*$', ' 111')# fails re.match(r'[\t\n\r\f]*\d\d*$', ' 111') # fails re.match(r'[\t\n\r\f]\d\d*$', ' 111') # fails re.match(r'[\t\n\r\f]*$', ' 111') # fails What is wrong in above script? Thanks -- https://mail.python.org/mailman/listinfo/python-list
Re: Why is it different about '\s' Matches whitespace and Equivalent to [\t\n\r\f]?
On 2014-07-10 11:05, rxjw...@gmail.com wrote: Hi, On a tutorial it says that '\s': Matches whitespace. Equivalent to [\t\n\r\f]. It's equivalent to [ \t\n\r\f], i.e. it also includes a space, so either the tutorial is wrong, or you didn't look closely enough. :-) I test it with: re.match(r'\s*\d\d*$', ' 111') _sre.SRE_Match object at 0x03642BB8 re.match(r'\t\n\r\f*\d\d*$', ' 111')# fails The string starts with ' ', not '\t'. re.match(r'[\t\n\r\f]*\d\d*$', ' 111') # fails re.match(r'[\t\n\r\f]\d\d*$', ' 111') # fails re.match(r'[\t\n\r\f]*$', ' 111') # fails The string starts with ' ', which isn't in the character set. What is wrong in above script? Thanks -- https://mail.python.org/mailman/listinfo/python-list
Re: Why is it different about '\s' Matches whitespace and Equivalent to [\t\n\r\f]?
On Thursday, July 10, 2014 7:18:01 AM UTC-4, MRAB wrote: On 2014-07-10 11:05, r...@gmail.com wrote: It's equivalent to [ \t\n\r\f], i.e. it also includes a space, so either the tutorial is wrong, or you didn't look closely enough. :-) The string starts with ' ', not '\t'. The string starts with ' ', which isn't in the character set. The '\s' description is on link: http://www.tutorialspoint.com/python/python_reg_expressions.htm Could you give me an example to use the equivalent pattern? Thanks -- https://mail.python.org/mailman/listinfo/python-list
Re: Why is it different about '\s' Matches whitespace and Equivalent to [\t\n\r\f]?
On 7/10/14 9:32 AM, fl wrote: On Thursday, July 10, 2014 7:18:01 AM UTC-4, MRAB wrote: On 2014-07-10 11:05, r...@gmail.com wrote: It's equivalent to [ \t\n\r\f], i.e. it also includes a space, so either the tutorial is wrong, or you didn't look closely enough. :-) The string starts with ' ', not '\t'. The string starts with ' ', which isn't in the character set. The '\s' description is on link: http://www.tutorialspoint.com/python/python_reg_expressions.htm For some reason, that page shows much of its information twice. The first occurrence of \s there is: \sMatches whitespace. Equivalent to [\t\n\r\f]. The second is: \sMatch a whitespace character: [ \t\r\n\f] The second one is correct. The first is wrong. You might want to send the author a bug report. Actually, neither is strictly correct, since as the official docs (https://docs.python.org/2/library/re.html) say, \sWhen the UNICODE flag is not specified, it matches any whitespace character, this is equivalent to the set [ \t\n\r\f\v]. The LOCALE flag has no extra effect on matching of the space. If UNICODE is set, this will match the characters [ \t\n\r\f\v] plus whatever is classified as space in the Unicode character properties database. Could you give me an example to use the equivalent pattern? Thanks -- Ned Batchelder, http://nedbatchelder.com -- https://mail.python.org/mailman/listinfo/python-list
Re: Why is it different about '\s' Matches whitespace and Equivalent to [\t\n\r\f]?
On 2014-07-10 14:32, fl wrote: On Thursday, July 10, 2014 7:18:01 AM UTC-4, MRAB wrote: It's equivalent to [ \t\n\r\f], i.e. it also includes a space, so either the tutorial is wrong, or you didn't look closely enough. :-) The string starts with ' ', not '\t'. The string starts with ' ', which isn't in the character set. The '\s' description is on link: http://www.tutorialspoint.com/python/python_reg_expressions.htm I can see that the space is missing. It should say: \sMatches whitespace. Equivalent to [ \t\n\r\f]. Could you give me an example to use the equivalent pattern? (I'm using Python 3.4, which is why the match object looks different.) import re re.match(r'\s*\d\d*$', ' 111') _sre.SRE_Match object; span=(0, 6), match=' 111' re.match(r'[ \t\n\r\f]*\d\d*$', ' 111') _sre.SRE_Match object; span=(0, 6), match=' 111' -- https://mail.python.org/mailman/listinfo/python-list
Why is it different from the example on the tutorial?
Hi, I type the following sample codes on Python, but it echoes differently. Regular expressions are compiled into pattern objects, which have methods for various operations such as searching for pattern matches or performing string substitutions. import re p = re.compile('ab*') p _sre.SRE_Pattern object at 0x... What I get on Python console: $ python Python 2.7.5 (default, Oct 2 2013, 22:34:09) [GCC 4.8.1] on cygwin Type help, copyright, credits or license for more information. import re p = re.compile('ab*') File stdin, line 1 p = re.compile('ab*') ^ SyntaxError: invalid syntax Thanks, -- https://mail.python.org/mailman/listinfo/python-list
Re: Why is it different from the example on the tutorial?
On 2014-07-06 05:13, rxjw...@gmail.com wrote: What I get on Python console: $ python Python 2.7.5 (default, Oct 2 2013, 22:34:09) [GCC 4.8.1] on cygwin Type help, copyright, credits or license for more information. import re p = re.compile('ab*') File stdin, line 1 p = re.compile('ab*') ^ SyntaxError: invalid syntax Are you sure that you copied/pasted that directly from the console instead of transcribing it with some mistake? I just did the same thing at the console and it worked perfectly fine $ python Python 2.7.3 (default, Mar 13 2014, 11:03:55) [GCC 4.7.2] on linux2 Type help, copyright, credits or license for more information. import re p = re.compile('ab*') -tkc -- https://mail.python.org/mailman/listinfo/python-list
Re: Why is it different from the example on the tutorial?
On Sunday, July 6, 2014 8:54:42 AM UTC-4, Tim Chase wrote: On 2014-07-06 05:13, rxjw...@gmail.com wrote: What I get on Python console: $ python Python 2.7.5 (default, Oct 2 2013, 22:34:09) [GCC 4.8.1] on cygwin Type help, copyright, credits or license for more information. import re p = re.compile('ab*') File stdin, line 1 p = re.compile('ab*') ^ SyntaxError: invalid syntax Are you sure that you copied/pasted that directly from the console instead of transcribing it with some mistake? I just did the same thing at the console and it worked perfectly fine $ python Python 2.7.3 (default, Mar 13 2014, 11:03:55) [GCC 4.7.2] on linux2 Type help, copyright, credits or license for more information. import re p = re.compile('ab*') -tkc Thanks. It did be caused by unclear copypaste. I shall be careful in future. When I enter: counter=100 counter 100 When I get match result: pattern='abcd' prog = re.compile(pattern) string='abcd' result = prog.match(string) result _sre.SRE_Match object at 0x6eda5e0 result.group(0) 'abcd' It looks like 'result' is different from a simple 'counter' variable. I do not yet find the definition of 'result' object. What do you call 'result' object? Where can I find it (what topic would be in a tutorial)? Thanks, -- https://mail.python.org/mailman/listinfo/python-list
Re: Why is it different from the example on the tutorial?
On Sunday, July 6, 2014 8:38:41 AM UTC-5, rxj...@gmail.com wrote: When I get match result: pypattern='abcd' pyprog = re.compile(pattern) pystring='abcd' pyresult = prog.match(string) pyresult _sre.SRE_Match object at 0x6eda5e0 pyresult.group(0) 'abcd' It looks like 'result' is different from a simple 'counter' variable. I do not yet find the definition of 'result' object. What do you call 'result' object? Where can I find it (what topic would be in a tutorial)? Thanks, One of the most powerful features of Python,,, for the noob,,, be documentation strings. With Python you need not buy expensive books, or venture into seedy and dangerous alley ways of the inter-webs, no, all you need to do is do that which any young and inexperienced lad would do when he finds himself in a troubling situation: YELL FOR HELP! The built-in function help will answer all your questions,,, considering you ask the correct questions of course!,,, but always remember the advice of a wise man and don't become the boy who cried wolf one too many times! -- https://mail.python.org/mailman/listinfo/python-list
Re: Why is it different from the example on the tutorial?
On 06/07/2014 14:38, rxjw...@gmail.com wrote: On Sunday, July 6, 2014 8:54:42 AM UTC-4, Tim Chase wrote: On 2014-07-06 05:13, rxjw...@gmail.com wrote: What I get on Python console: $ python Python 2.7.5 (default, Oct 2 2013, 22:34:09) [GCC 4.8.1] on cygwin Type help, copyright, credits or license for more information. import re p = re.compile('ab*') File stdin, line 1 p = re.compile('ab*') ^ SyntaxError: invalid syntax Are you sure that you copied/pasted that directly from the console instead of transcribing it with some mistake? I just did the same thing at the console and it worked perfectly fine $ python Python 2.7.3 (default, Mar 13 2014, 11:03:55) [GCC 4.7.2] on linux2 Type help, copyright, credits or license for more information. import re p = re.compile('ab*') -tkc Thanks. It did be caused by unclear copypaste. I shall be careful in future. When I enter: counter=100 counter 100 When I get match result: pattern='abcd' prog = re.compile(pattern) string='abcd' result = prog.match(string) result _sre.SRE_Match object at 0x6eda5e0 result.group(0) 'abcd' It looks like 'result' is different from a simple 'counter' variable. I do not yet find the definition of 'result' object. What do you call 'result' object? Where can I find it (what topic would be in a tutorial)? Thanks, help(result) Help on SRE_Match object: class SRE_Match(builtins.object) | The result of re.match() and re.search(). etc https://docs.python.org/3/library/re.html#module-re https://docs.python.org/3/library/re.html#match-objects https://docs.python.org/3/library/re.html#re.match https://docs.python.org/3/library/re.html#re.search https://docs.python.org/3/library/re.html#regular-expression-examples A slight aside, would you please use the mailing list https://mail.python.org/mailman/listinfo/python-list or read and action this https://wiki.python.org/moin/GoogleGroupsPython to prevent us seeing double line spacing and single line paragraphs, thanks. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com -- https://mail.python.org/mailman/listinfo/python-list
Re: Why is it different from the example on the tutorial?
In article 21e704ee-648b-423d-8682-11cb310a3...@googlegroups.com, Rick Johnson rantingrickjohn...@gmail.com wrote: On Sunday, July 6, 2014 8:38:41 AM UTC-5, rxj...@gmail.com wrote: When I get match result: pypattern='abcd' pyprog = re.compile(pattern) pystring='abcd' pyresult = prog.match(string) pyresult _sre.SRE_Match object at 0x6eda5e0 pyresult.group(0) 'abcd' It looks like 'result' is different from a simple 'counter' variable. I do not yet find the definition of 'result' object. What do you call 'result' object? Where can I find it (what topic would be in a tutorial)? Thanks, One of the most powerful features of Python,,, for the noob,,, be documentation strings. I guess I must still be a noob, because I still find them pretty useful! More generically, Python supports introspection, which means you can ask an object to tell you things about itself. Let's say you've got an object, foo. Here's some useful things you can do to learn more about it: * As Rick points out, you can do help(foo). This is probably the place to start. * You can print dict(foo), which just prints out the attributes the object has. This is really handy when you vaguely remember that a class has some operation, but can't remember the exact name. For example, I use two different database packages, and can never remember which has sort() and which has order_by(). * You can print foo itself, to find out its value, but this can get tricky, since sometimes objects print themselves in confusing ways. Printing repr(foo) will usually get you more detail. * You can print type(foo), to find out exactly what it is (useful when even printing repr() doesn't explain what's going on). -- https://mail.python.org/mailman/listinfo/python-list
Re: Why is it different from the example on the tutorial?
On Mon, Jul 7, 2014 at 12:34 AM, Roy Smith r...@panix.com wrote: * You can print type(foo), to find out exactly what it is (useful when even printing repr() doesn't explain what's going on). And very VERY occasionally, print(id(type(foo))) comes in handy, because two types might look the same, but an isinstance check looks (modulo subclassing) at type identity. :) But yes, the info Roy listed is normally what you'll be wanting. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Why is it different from the example on the tutorial?
On 06/07/2014 15:34, Roy Smith wrote: * You can print foo itself, to find out its value, but this can get tricky, since sometimes objects print themselves in confusing ways. Printing repr(foo) will usually get you more detail. For the OP the pretty print module is usually better than plain old print https://docs.python.org/3/library/pprint.html#module-pprint. I use it like this from pprint import pprint as pp The iPython shell (and presumably others as well) also does a better job of displaying objects than print. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com -- https://mail.python.org/mailman/listinfo/python-list
Re: Why is it different from the example on the tutorial?
On Sunday, July 6, 2014 10:18:53 AM UTC-4, Rick Johnson wrote: On Sunday, July 6, 2014 8:38:41 AM UTC-5, rxj...@gmail.com wrote: When I get match result: pypattern='abcd' pyprog = re.compile(pattern) pystring='abcd' pyresult = prog.match(string) pyresult _sre.SRE_Match object at 0x6eda5e0 pyresult.group(0) 'abcd' It looks like 'result' is different from a simple 'counter' variable. I do not yet find the definition of 'result' object. What do you call 'result' object? Where can I find it (what topic would be in a tutorial)? Thanks, One of the most powerful features of Python,,, for the noob,,, be documentation strings. With Python you need not buy expensive books, or venture into seedy and dangerous alley ways of the inter-webs, no, all you need to do is do that which any young and inexperienced lad would do when he finds himself in a troubling situation: YELL FOR HELP! The built-in function help will answer all your questions,,, considering you ask the correct questions of course!,,, but always remember the advice of a wise man and don't become the boy who cried wolf one too many times! Thanks. I do not want to waste everyone's time. For a jump start, there are small errors making me frustrating. Your help does help me, confirm the usage etc. After a basic familiarity, I do not want to post more. I use cygwin Python, I type help of an object 'result'. It does show up the help content, but it never quits the help afterwards. It is annoying, and time wasting. (Sorry again, that problem may be about Cygwin, not Python. Excuse me to mention that here. Of course, your help post consumes your time. Thanks again. -- https://mail.python.org/mailman/listinfo/python-list
Re: Why is it different from the example on the tutorial?
rxjw...@gmail.com wrote: I use cygwin Python, I type help of an object 'result'. It does show up the help content, but it never quits the help afterwards. It is annoying, and time wasting. To quit help try hitting the 'q' key. -- https://mail.python.org/mailman/listinfo/python-list
Re: Why is it different from the example on the tutorial?
On Sunday, July 6, 2014 9:34:44 AM UTC-5, Roy Smith wrote: * You can print dict(foo), which just prints out the attributes the object has. Looks like a typo there. I think you probably meant to say dir(foo) INTERACTIVE SESSION: Python 2.x py l = range(5) py l [0, 1, 2, 3, 4] py dict(l) Traceback (most recent call last): File pyshell#2, line 1, in module dict(l) TypeError: cannot convert dictionary update sequence element #0 to a sequence py dir(l) ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] -- https://mail.python.org/mailman/listinfo/python-list
Re: Why is it different from the example on the tutorial?
In article 6fd77d6a-3487-474b-bb96-8da6ab800...@googlegroups.com, Rick Johnson rantingrickjohn...@gmail.com wrote: On Sunday, July 6, 2014 9:34:44 AM UTC-5, Roy Smith wrote: * You can print dict(foo), which just prints out the attributes the object has. Looks like a typo there. I think you probably meant to say dir(foo) This is true. Good catch. -- https://mail.python.org/mailman/listinfo/python-list
Re: Why is it different from the example on the tutorial?
On Sunday, July 6, 2014 10:03:48 AM UTC-5, rxj...@gmail.com wrote: Thanks. I do not want to waste everyone's time. Oh NOW you tell us! I could be ranting about flashlights, but here i am wasting time with you again! For a jump start, there are small errors making me frustrating. Your help does help me, confirm the usage etc. How about YOU confirm your OWN usage. I am confident the help function works just fine, can you prove otherwise? We're here to help not to write code for you like slaves. Remember GIGO! After a basic familiarity, I do not want to post more. I use cygwin Python, I type help of an object 'result'. It does show up the help content, but it never quits the help afterwards. It is annoying, and time wasting. Surely you have a simple Python command line available? Last i heard GvR was giving them away for free! Can you open one and try some interactive musings? If the only tool in your toolbox is a hammer, well, you know the rest... -- https://mail.python.org/mailman/listinfo/python-list
Re: Why is it different from the example on the tutorial?
On Sunday, July 6, 2014 5:43:55 PM UTC+5:30, rxj...@gmail.com wrote: Hi, I type the following sample codes on Python, but it echoes differently. Regular expressions are compiled into pattern objects, which have methods for various operations such as searching for pattern matches or performing string substitutions. import re p = re.compile('ab*') p What I get on Python console: $ python Python 2.7.5 (default, Oct 2 2013, 22:34:09) [GCC 4.8.1] on cygwin Type help, copyright, credits or license for more information. import re p = re.compile('ab*') File stdin, line 1 p = re.compile('ab*') ^ SyntaxError: invalid syntax 1. For *using* regular exps match is fine For *hacking in the interpreter* I find findall more convenient. match and findall take the same arguments 2. I wouldn't bother with compile at least at the start 3. Use raw strings for patterns even if it does not seem necessary (below) $ python Python 2.7.7 (default, Jun 3 2014, 16:16:56) [GCC 4.8.3] on linux2 Type help, copyright, credits or license for more information. from re import findall findall(r'ab*', 'abcd') ['ab'] findall(r'ab*', 'abcdab') ['ab', 'ab'] findall(r'ab*', 'abcdabbb') ['ab', 'abbb'] -- https://mail.python.org/mailman/listinfo/python-list
Re: Why is it different from the example on the tutorial?
On 06/07/2014 16:03, rxjw...@gmail.com wrote: Thanks. I do not want to waste everyone's time. For a jump start, there are small errors making me frustrating. Your help does help me, confirm the usage etc. After a basic familiarity, I do not want to post more. I use cygwin Python, I type help of an object 'result'. It does show up the help content, but it never quits the help afterwards. It is annoying, and time wasting. (Sorry again, that problem may be about Cygwin, not Python. Excuse me to mention that here. Of course, your help post consumes your time. Thanks again. Don't bother about wasting our time as that's what we're here for :) There is a tutor mailing list that you might feel more comfortable on. See https://mail.python.org/mailman/listinfo/tutor or gmane.comp.python.tutor -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com -- https://mail.python.org/mailman/listinfo/python-list
Re: Why is it different from the example on the tutorial?
On 07/06/2014 08:03 AM, rxjw...@gmail.com wrote: snip Thanks. I do not want to waste everyone's time. For a jump start, there are small errors making me frustrating. Your help does help me, confirm the usage etc. After a basic familiarity, I do not want to post more. I use cygwin Python, I type help of an object 'result'. It does show up the help content, but it never quits the help afterwards. It is annoying, and time wasting. (Sorry again, that problem may be about Cygwin, not Python. Excuse me to mention that here. Of course, your help post consumes your time. Thanks again. I'm curious as to why you're using Cygwin Python. Cygwin is great for using Unix/Linux programs that are not available in Windows, but there are Windows-native versions of Python available. I would suggest that you would be better off installing Python directly into Windows instead of going round-about with Cygwin. If you do this, I would also suggest that you install Python 3 instead of Python 2, but you may have to find a different tutorial. Although I suspect that part of your problems are that the tutorial you're using IS for Python 3 rather than the 2.7 you are using. Really, there aren't a lot of differences between 2 and 3, but the differences ARE very significant. And 3 is definitely the better, as well as the future. (Aside: I do have Cygwin installed on my Windows system, but I have pretty much given up on using Windows. I find Linux far superior and more comfortable to use, and it gets away from Big-Brotherish Microsoft. I do use it occasionally, but I think the last time I ran it was at least two months ago.) -=- Larry -=- -- https://mail.python.org/mailman/listinfo/python-list
Re: Why is it different from the example on the tutorial?
On Sunday, July 6, 2014 4:32:14 PM UTC-4, Larry Hudson wrote: On 07/06/2014 08:03 AM, rxjw...@gmail.com wrote: snip Thanks. I do not want to waste everyone's time. For a jump start, there are small errors making me frustrating. Your help does help me, confirm the usage etc. After a basic familiarity, I do not want to post more. I use cygwin Python, I type help of an object 'result'. It does show up the help content, but it never quits the help afterwards. It is annoying, and time wasting. (Sorry again, that problem may be about Cygwin, not Python. Excuse me to mention that here. Of course, your help post consumes your time. Thanks again. I'm curious as to why you're using Cygwin Python. Cygwin is great for using Unix/Linux programs that are not available in Windows, but there are Windows-native versions of Python available. I would suggest that you would be better off installing Python directly into Windows instead of going round-about with Cygwin. If you do this, I would also suggest that you install Python 3 instead of Python 2, but you may have to find a different tutorial. Although I suspect that part of your problems are that the tutorial you're using IS for Python 3 rather than the 2.7 you are using. Really, there aren't a lot of differences between 2 and 3, but the differences ARE very significant. And 3 is definitely the better, as well as the future. (Aside: I do have Cygwin installed on my Windows system, but I have pretty much given up on using Windows. I find Linux far superior and more comfortable to use, and it gets away from Big-Brotherish Microsoft. I do use it occasionally, but I think the last time I ran it was at least two months ago.) -=- Larry -=- Thanks. In fact, I have both Cygwin and Windows version Python, but I incorrectly thought that Cygwin version Python would be closer to Linux than Windows Python. Another thing, I find that Windows Python have a GUI interface. It is good, but it does not have command history as Cygwin has. -- https://mail.python.org/mailman/listinfo/python-list
why is this different?
Hello snakes :) In [38]: f = [lambda:i for i in range(10)] In [39]: ff = map(lambda i: lambda : i, range(10)) In [40]: f[0]() Out[40]: 9 In [41]: f[1]() Out[41]: 9 In [42]: ff[0]() Out[42]: 0 In [43]: ff[1]() Out[43]: 1 I don't understand why in the first case f[for all i in 0..9]==9 what is different from (more usefull) In [44]: f = [%i % i for i in range(10)] In [45]: f Out[45]: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] doing it like this works again In [54]: def d(x): : return lambda:x : In [55]: f = [d(i) for i in range(10)] In [56]: f[0]() Out[56]: 0 In [57]: f[1]() Out[57]: 1 in a C programmer sence I would say there seems to be no sequence point which would separate now from next Regards, Daniel -- http://mail.python.org/mailman/listinfo/python-list
Re: why is this different?
On 7 dic, 22:53, Schüle Daniel [EMAIL PROTECTED] wrote: In [38]: f = [lambda:i for i in range(10)] In [39]: ff = map(lambda i: lambda : i, range(10)) In [40]: f[0]() Out[40]: 9 In [41]: f[1]() Out[41]: 9 In [42]: ff[0]() Out[42]: 0 In [43]: ff[1]() Out[43]: 1 I don't understand why in the first case f[for all i in 0..9]==9 In the first case, i is a free variable. That means that Python will get it from other place (the global namespace, likely [surely?]) f=[lambda:i for i in range(10)] f[0]() 9 i=123 f[0]() 123 print f[0].func_closure None In the second case, the inner i is a free variable, but local to its enclosing scope (outer lambda). It's a closure: ff = map(lambda i: lambda : i, range(10)) ff[4]() 4 ff[4].func_closure (cell at 0x00ACEA90: int object at 0x00995344,) i=321 ff[4]() 4 what is different from (more usefull) In [44]: f = [%i % i for i in range(10)] In [45]: f Out[45]: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] This is a simple expression evaluated at each iteration doing it like this works again In [54]: def d(x): : return lambda:x : x inside lambda is a free variable, but since it's local to d, this becomes a closure: d(1) function lambda at 0x00A35EF0 d(1).func_closure (cell at 0x00A2A4F0: int object at 0x00995338,) In [55]: f = [d(i) for i in range(10)] In [56]: f[0]() Out[56]: 0 In [57]: f[1]() Out[57]: 1 This is similar to the ff example above in a C programmer sence I would say there seems to be no sequence point which would separate now from next No, the problem is that C has no way to express a closure; this is a functional concept absolutely extraneous to the C language. -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: why is this different?
Gabriel Genellina schrieb: Gabriel Genellina schrieb: On 7 dic, 22:53, Schüle Daniel [EMAIL PROTECTED] wrote: In [38]: f = [lambda:i for i in range(10)] In [39]: ff = map(lambda i: lambda : i, range(10)) In [40]: f[0]() Out[40]: 9 In [41]: f[1]() Out[41]: 9 In [42]: ff[0]() Out[42]: 0 In [43]: ff[1]() Out[43]: 1 I don't understand why in the first case f[for all i in 0..9]==9 In the first case, i is a free variable. That means that Python will get it from other place (the global namespace, likely [surely?]) f=[lambda:i for i in range(10)] f[0]() 9 i=123 f[0]() 123 print f[0].func_closure None In the second case, the inner i is a free variable, but local to its enclosing scope (outer lambda). It's a closure: ff = map(lambda i: lambda : i, range(10)) ff[4]() 4 ff[4].func_closure (cell at 0x00ACEA90: int object at 0x00995344,) i=321 ff[4]() 4 what is different from (more usefull) In [44]: f = [%i % i for i in range(10)] In [45]: f Out[45]: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] This is a simple expression evaluated at each iteration doing it like this works again In [54]: def d(x): : return lambda:x : x inside lambda is a free variable, but since it's local to d, this becomes a closure: d(1) function lambda at 0x00A35EF0 d(1).func_closure (cell at 0x00A2A4F0: int object at 0x00995338,) In [55]: f = [d(i) for i in range(10)] In [56]: f[0]() Out[56]: 0 In [57]: f[1]() Out[57]: 1 This is similar to the ff example above in a C programmer sence I would say there seems to be no sequence point which would separate now from next No, the problem is that C has no way to express a closure; this is a functional concept absolutely extraneous to the C language. On 7 dic, 22:53, Schüle Daniel [EMAIL PROTECTED] wrote: In [38]: f = [lambda:i for i in range(10)] In [39]: ff = map(lambda i: lambda : i, range(10)) In [40]: f[0]() Out[40]: 9 In [41]: f[1]() Out[41]: 9 In [42]: ff[0]() Out[42]: 0 In [43]: ff[1]() Out[43]: 1 I don't understand why in the first case f[for all i in 0..9]==9 In the first case, i is a free variable. That means that Python will get it from other place (the global namespace, likely [surely?]) f=[lambda:i for i in range(10)] f[0]() 9 i=123 f[0]() 123 print f[0].func_closure None intersting I think I got it [] I have two more examples, but now I understand the difference In [70]: x = [eval(lambda:i) for i in range(10)] In [71]: y = [eval(lambda:%i % i) for i in range(10)] I think [71] is most obvious what the programmer intends Thx -- http://mail.python.org/mailman/listinfo/python-list
Re: why is this different?
In [EMAIL PROTECTED], Schüle Daniel wrote: I have two more examples, but now I understand the difference In [70]: x = [eval(lambda:i) for i in range(10)] In [71]: y = [eval(lambda:%i % i) for i in range(10)] I think [71] is most obvious what the programmer intends But unnecessary use of `eval()` is evil. You can spell it this way: z = [lambda x=i: x for i in range(10)] The `x` is a local name and default values are evaluated and bound when the function is created. Ciao, Marc 'BlackJack' Rintsch -- http://mail.python.org/mailman/listinfo/python-list
Re: why is this different?
On 7 dic, 22:53, Schüle Daniel [EMAIL PROTECTED] wrote: In [38]: f = [lambda:i for i in range(10)] In [39]: ff = map(lambda i: lambda : i, range(10)) In [40]: f[0]() Out[40]: 9 In [41]: f[1]() Out[41]: 9 In [42]: ff[0]() Out[42]: 0 In [43]: ff[1]() Out[43]: 1 I don't understand why in the first case f[for all i in 0..9]==9 In the first case, i is a free variable. That means that Python will get it from other place (the global namespace, likely [surely?]) f=[lambda:i for i in range(10)] f[0]() 9 i=123 f[0]() 123 print f[0].func_closure None In the second case, the inner i is a free variable, but local to its enclosing scope (outer lambda). It's a closure: ff = map(lambda i: lambda : i, range(10)) ff[4]() 4 ff[4].func_closure (cell at 0x00ACEA90: int object at 0x00995344,) i=321 ff[4]() 4 what is different from (more usefull) In [44]: f = [%i % i for i in range(10)] In [45]: f Out[45]: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] This is a simple expression evaluated at each iteration doing it like this works again In [54]: def d(x): : return lambda:x : x inside lambda is a free variable, but since it's local to d, this becomes a closure: d(1) function lambda at 0x00A35EF0 d(1).func_closure (cell at 0x00A2A4F0: int object at 0x00995338,) In [55]: f = [d(i) for i in range(10)] In [56]: f[0]() Out[56]: 0 In [57]: f[1]() Out[57]: 1 This is similar to the ff example above in a C programmer sence I would say there seems to be no sequence point which would separate now from next No, the problem is that C has no way to express a closure; this is a functional concept absolutely extraneous to the C language. -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list