Re: [Python-Dev] List vs Tuple / Homogeneous vs Heterogeneous / Mutable vs Immutable
> > In teaching Python, I find that analogs to other languages are helpful > in explaining Python even if a person doesn't know the other language. > sorted(set(open(somefile))) > is like: > cat somefile | sort | uniq # different algorithm, same outcome > or: >SELECT DISTINCT line FROM somelines ORDER BY line; I think that it helps by making the learning curve less steep, although it also has the potential to produce poor code because different languages may have different idioms to solving different problems. Much like if try to think in English and translate word by word to Spanish, for example (you'll probably get by, but it won't really be like how a native in that language does). Leandro 2014-04-22 3:39 GMT-03:00 Raymond Hettinger : > > On Apr 18, 2014, at 1:21 AM, Jeff Allen wrote: > > The "think of tuples like a struct in C" explanation immediately reminded > me that ... > > On 16/04/2014 21:42, Taavi Burns wrote (in his excellent notes from the > language summit): > > The demographics have changed. How do > we change the docs and ecosystem to avoid the assumption that Python > programmers already know how to program in C? > > > In teaching Python, I find that analogs to other languages are helpful > in explaining Python even if a person doesn't know the other language. > > sorted(set(open(somefile))) > > is like: > > cat somefile | sort | uniq # different algorithm, same outcome > > or: > >SELECT DISTINCT line FROM somelines ORDER BY line; > > In particular, it is effective to say that tuples are used like structs in > other languages > or like records in database tables. > > I don't think we should go down the road of removing all the similes and > metaphors > from the docs. While children don't usually benefit from them, adults > face different > learning challenges. Usually, their biggest learning hurdle is > figuring-out what is > and what is not a valid comparison to other things they already know. > > One problem I have seen with the usual list vs tuple explanations is that > they seem to suggest that the list-are-for-looping and > tuples-are-like-records > notions are baked into the implementation of lists and tuples. > > In fact, the distinction is extrinsic to their implementations. It is > only important > because the rest of the language tends to treat them differently. For > example, > you could store ['raymond', 'red'] as a list or as a tuple ('raymond', > 'red'), but you > wouldn't be punished until later when you tried: > > 'I think %s likes %s' % container # str.__mod__ treats lists and > tuples differently > > Likewise, there seems to be wide-spread confusion about make makes an > object immutable. People seem to miss that ints, tuples, None and str are > immutable only because they lack any mutating methods, > > > Raymond > > > > ___ > Python-Dev mailing list > Python-Dev@python.org > https://mail.python.org/mailman/listinfo/python-dev > Unsubscribe: > https://mail.python.org/mailman/options/python-dev/leandropls%40cpti.cetuc.puc-rio.br > > ___ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] List vs Tuple / Homogeneous vs Heterogeneous / Mutable vs Immutable
That's rather vague, isn't it? "Usually contains" isn't nearly as prescriptive as "should be used for". A co-worker with whom I discussed the matter these days also argued that a language shouldn't prescribe as one uses a data structure, although I do think conventions in semantics helps maintainability. Leandro 2014-04-18 16:59 GMT-03:00 Ezio Melotti : > Hi, > > On Thu, Apr 17, 2014 at 10:23 PM, Guido van Rossum > wrote: > > It's definitely something that should be put in some documentation, > > see http://bugs.python.org/issue14840 and > https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences > : > """ > Though tuples may seem similar to lists, they are often used in > different situations and for different purposes. Tuples are immutable, > and usually contain an heterogeneous sequence of elements that are > accessed via unpacking (see later in this section) or indexing (or > even by attribute in the case of namedtuples). Lists are mutable, and > their elements are usually homogeneous and are accessed by iterating > over the list. > """ > > Best Regards, > Ezio Melotti > > > probably > > at the point when people have learned enough to be designing their own > > programs where this issue comes up -- before they're wizards but well > after > > they have learned the semantic differences between lists and tuples. > > > > > > On Thu, Apr 17, 2014 at 11:49 AM, Brett Cannon > wrote: > >> > >> > >> > >> On Thu Apr 17 2014 at 2:43:35 PM, Leandro Pereira de Lima e Silva > >> wrote: > >>> > >>> Hello there! > >>> > >>> I've stumbled upon this discussion on python-dev about what the choice > >>> between using a list or a tuple is all about in 2003: > >>> 1. https://mail.python.org/pipermail/python-dev/2003-March/033962.html > >>> 2. https://mail.python.org/pipermail/python-dev/2003-March/034029.html > >>> > >>> There's a vague comment about it on python documentation but afaik > there > >>> the discussion hasn't made into any PEPs. Is there an understanding > about > >>> it? > >> > >> > >> Think of tuples like a struct in C, lists like an array. That's just out > >> of Guido's head so I don't think we have ever bothered to write it down > >> somewhere as an important distinction of the initial design that should > be > >> emphasized. > >> > >> ___ > >> Python-Dev mailing list > >> Python-Dev@python.org > >> https://mail.python.org/mailman/listinfo/python-dev > >> Unsubscribe: > >> https://mail.python.org/mailman/options/python-dev/guido%40python.org > >> > > > > > > > > -- > > --Guido van Rossum (python.org/~guido) > > > > ___ > > Python-Dev mailing list > > Python-Dev@python.org > > https://mail.python.org/mailman/listinfo/python-dev > > Unsubscribe: > > > https://mail.python.org/mailman/options/python-dev/ezio.melotti%40gmail.com > > > ___ > Python-Dev mailing list > Python-Dev@python.org > https://mail.python.org/mailman/listinfo/python-dev > Unsubscribe: > https://mail.python.org/mailman/options/python-dev/leandropls%40cpti.cetuc.puc-rio.br > ___ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] List vs Tuple / Homogeneous vs Heterogeneous / Mutable vs Immutable
Leandro Pereira de Lima e Silva writes: >> In teaching Python, I find that analogs to other languages are >> helpful in explaining Python even if a person doesn't know the other >> language. >> sorted(set(open(somefile))) >> is like: >> cat somefile | sort | uniq # different algorithm, same outcome >> or: >> SELECT DISTINCT line FROM somelines ORDER BY line; > I think that it helps by making the learning curve less steep, > although it also has the potential to produce poor code because > different languages may have different idioms to solving different > problems. True, of course. I think what Raymond is saying is that these other-language examples can help smooth over those "¿Qué pasa?" moments, so the teacher can focus on style and idiomatic coding in the language at hand. Some teachers will use them more than others do. On the other hand, there's no need to purge them from the docs and tutorials. If someone notices that some example/analogy seems likely to get in the way of understanding, they can and should write a patch to improve that example. ___ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com