Re: [Tutor] list semantics
On Sat, Apr 11, 2015 at 3:21 PM, Steven D'Aprano wrote: > On Sun, Apr 12, 2015 at 05:09:43AM +1000, Steven D'Aprano wrote: > >> Almost correct, but not quite. range, like xrange in Python 2, is not a >> generator, but a custom-made lazy sequence object. >> >> py> gen() # This actually is a generator. >> >> py> range(1, 10) # This is not. >> range(1, 10) > > Oops, I forgot to show where gen() came from. Sorry about that, just a > cut-and-paste error. > > py> def gen(): > ... yield 1 > ... > py> gen() # This actually is a generator. > > Thanks for the tip Steve > > > -- > Steve > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor -- Joel Goldstick http://joelgoldstick.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] list semantics
On Sun, Apr 12, 2015 at 05:09:43AM +1000, Steven D'Aprano wrote: > Almost correct, but not quite. range, like xrange in Python 2, is not a > generator, but a custom-made lazy sequence object. > > py> gen() # This actually is a generator. > > py> range(1, 10) # This is not. > range(1, 10) Oops, I forgot to show where gen() came from. Sorry about that, just a cut-and-paste error. py> def gen(): ... yield 1 ... py> gen() # This actually is a generator. -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] list semantics
On 11/04/2015 20:02, Steven D'Aprano wrote: On Sat, Apr 11, 2015 at 10:41:28AM -0700, Jim Mooney wrote: Why does the first range convert to a list, but not the second? p = list(range(1,20)), (range(40,59)) p ([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19], range(40, 59)) Why would the second convert to a list? You don't call list() on it. You create a tuple, p, with two items. The first item is: list(range(1, 20)) and the second item is: range(40, 59) so you end up with p being a tuple ([1, 2, 3, ..., 19], range(40, 59)). The fact that you surround the second item with round brackets (parentheses) means nothing -- they just group the range object on its own. A bit like saying 1 + (2), which still evaluates as 3. To follow up the tuple is created with a comma *NOT* parenthesis. So in the example it is the second comma, the one immediately after the call to list(), that makes p a tuple. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] list semantics
On Sat, Apr 11, 2015 at 02:15:49PM -0400, Joel Goldstick wrote: > On Sat, Apr 11, 2015 at 1:41 PM, Jim Mooney wrote: > > Why does the first range convert to a list, but not the second? > > > p = list(range(1,20)), (range(40,59)) > p > > ([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19], > > range(40, 59)) > > > Assuming you are using python 3.x range is a generator, so it only > produces values if you iterate over it Almost correct, but not quite. range, like xrange in Python 2, is not a generator, but a custom-made lazy sequence object. py> gen() # This actually is a generator. py> range(1, 10) # This is not. range(1, 10) Range objects are special: not only do they produce values lazily as needed, but they also support len(), indexing, slicing, and membership testing, none of which generators are capable of doing: py> r = range(1, 30, 3) py> len(r) 10 py> r[8] # indexing 25 py> r[2:5] # slicing range(7, 16, 3) py> 9 in r # membership testing False py> 10 in r True All those results are calculated lazily, without having to pre-calculate a potentially enormous list. -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] list semantics
On Sat, Apr 11, 2015 at 10:41:28AM -0700, Jim Mooney wrote: > Why does the first range convert to a list, but not the second? > > >>> p = list(range(1,20)), (range(40,59)) > >>> p > ([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19], > range(40, 59)) Why would the second convert to a list? You don't call list() on it. You create a tuple, p, with two items. The first item is: list(range(1, 20)) and the second item is: range(40, 59) so you end up with p being a tuple ([1, 2, 3, ..., 19], range(40, 59)). The fact that you surround the second item with round brackets (parentheses) means nothing -- they just group the range object on its own. A bit like saying 1 + (2), which still evaluates as 3. -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] list semantics
Op 11-04-15 om 19:41 schreef Jim Mooney: Why does the first range convert to a list, but not the second? p = list(range(1,20)), (range(40,59)) p ([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19], range(40, 59)) I'm not sure I understand correctly. This is what the top of help(range) says: < Help on class range in module builtins: class range(object) | range(stop) -> range object | range(start, stop[, step]) -> range object | | Return a virtual sequence of numbers from start to stop by step. > So range() returns a range object. In your example you convert the first one to a list with list(), but not the second, so it prints the range object. >>> range(2) range(0, 2) >>> list(range(2)) [0, 1] Timo ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] list semantics
On Sat, Apr 11, 2015 at 1:41 PM, Jim Mooney wrote: > Why does the first range convert to a list, but not the second? > p = list(range(1,20)), (range(40,59)) p > ([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19], > range(40, 59)) > Assuming you are using python 3.x range is a generator, so it only produces values if you iterate over it > -- > Jim > > "Stop, Harold! That bagel has radishes!" > "Thank God, Mary - you've saved me again!" > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor -- Joel Goldstick http://joelgoldstick.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] list semantics
Why does the first range convert to a list, but not the second? >>> p = list(range(1,20)), (range(40,59)) >>> p ([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19], range(40, 59)) -- Jim "Stop, Harold! That bagel has radishes!" "Thank God, Mary - you've saved me again!" ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor