Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Help understanding the decisions *behind* python?

Reply
Thread Tools

Help understanding the decisions *behind* python?

 
 
Phillip B Oldham
Guest
Posts: n/a
 
      07-20-2009
My colleagues and I have been working with python for around 6 months
now, and while we love a lot of what python has done for us and what
it enables us to do some of the decisions behind such certain
data-types and their related methods baffle us slightly (when compared
to the decisions made in other, similarly powerful languages).

Specifically the "differences" between lists and tuples have us
confused and have caused many "discussions" in the office. We
understand that lists are mutable and tuples are not, but we're a
little lost as to why the two were kept separate from the start. They
both perform a very similar job as far as we can tell.

Consider the following:

>>> x = [2,1,3]
>>> x.sort()
>>> print x

[1, 2, 3]

Now, if the sort operations were unable to affect the original
structure of the list (as in JavaScript) you'd effectively have a
tuple which you could add/remove from, and the example above would
look more like:

>>> x = [2,1,3]
>>> print x.sort()

[1, 2, 3]
>>> print x

[2,1,3]

This make a lot more sense to us, and follows the convention from
other languages. It would also mean chaining methods to manipulate
lists would be easier:

>>> x = [2,1,3]
>>> print x.sort()[0]

3
>>> print x

[2,1,3]

We often find we need to do manipulations like the above without
changing the order of the original list, and languages like JS allow
this. We can't work out how to do this in python though, other than
duplicating the list, sorting, reversing, then discarding.

We're not looking to start any arguments or religious wars and we're
not asking that python be changed into something its not. We'd simply
like to understand the decision behind the lists and tuple structures.
We feel that in not "getting" the difference between the two types we
may be missing out on using these data structures to their full
potential.
 
Reply With Quote
 
 
 
 
Anthony Tolle
Guest
Posts: n/a
 
      07-20-2009
On Jul 20, 12:27*pm, Phillip B Oldham <phillip.old...@gmail.com>
wrote:
> ...
> Specifically the "differences" between lists and tuples have us
> confused and have caused many "discussions" in the office. We
> understand that lists are mutable and tuples are not, but we're a
> little lost as to why the two were kept separate from the start. They
> both perform a very similar job as far as we can tell.
> ...


There's no hard-set rules, but tuples are typically used to hold
collections of heterogeneous data, e.g. (10, "spam", 3.21). As has
been mentioned, such a tuple can be used as a dictionary key, whereas
a list cannot be used as a dictionary key, because it is mutable.

Lists, on the other hand, typically hold collections of homogeneous
data, e.g. [1, 2, 5] or ["spam", "eggs", "sausage"].

Of course, you'll also see plenty of examples of tuples containing
homogeneous data and lists containing heterogeneous data
 
Reply With Quote
 
 
 
 
Phillip B Oldham
Guest
Posts: n/a
 
      07-20-2009
On Jul 20, 6:08*pm, Duncan Booth <duncan.bo...@invalid.invalid> wrote:
> The main reason why you need both lists and tuples is that because a tuple
> of immutable objects is itself immutable you can use it as a dictionary
> key.


Really? That sounds interesting, although I can't think of any real-
world cases where you'd use something like that.
 
Reply With Quote
 
J. Cliff Dyer
Guest
Posts: n/a
 
      07-20-2009
On Mon, 2009-07-20 at 12:26 -0700, Phillip B Oldham wrote:
> On Jul 20, 6:08 pm, Duncan Booth <duncan.bo...@invalid.invalid> wrote:
> > The main reason why you need both lists and tuples is that because a tuple
> > of immutable objects is itself immutable you can use it as a dictionary
> > key.

>
> Really? That sounds interesting, although I can't think of any real-
> world cases where you'd use something like that.


Well, if you wanted to index a dictionary by coordinates, you might do
something like this:


fleet = {}
fleet[9,4] = 'destroyer'
fleet[8,4] = 'destroyer'
fleet[3,5] = 'aircraftcarrier'
fleet[4,5] = 'aircraftcarrier'
fleet[5,5] = 'aircraftcarrier'
fleet[6,5] = 'aircraftcarrier'
fleet[8,0] = 'battleship'
fleet[8,1] = 'battleship'
fleet[8,2] = 'battleship'


def checkattack(x, y, fleet):
if x,y in fleet:
return "You hit my %s' % fleet[x,y]

Maybe not the best implementation of Battleship, but you get the idea.



 
Reply With Quote
 
Piet van Oostrum
Guest
Posts: n/a
 
      07-20-2009
>>>>> Duncan Booth <> (DB) wrote:

>DB> Phillip B Oldham <> wrote:
>>> This make a lot more sense to us, and follows the convention from
>>> other languages. It would also mean chaining methods to manipulate
>>> lists would be easier:
>>>
>>>>>> x = [2,1,3]
>>>>>> print x.sort()[0]
>>> 3
>>>>>> print x
>>> [2,1,3]


>DB> You already have a way to do what you want:


>>>>> x = [2,1,3]
>>>>> print sorted(x)[0]

>DB> 3


What kind of Python produces that?
--
Piet van Oostrum <>
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email:
 
Reply With Quote
 
Chris Rebert
Guest
Posts: n/a
 
      07-20-2009
On Mon, Jul 20, 2009 at 2:23 PM, Piet van Oostrum<> wrote:
>>>>>> Duncan Booth <> (DB) wrote:

>
>>DB> Phillip B Oldham <> wrote:
>>>> This make a lot more sense to us, and follows the convention from
>>>> other languages. It would also mean chaining methods to manipulate
>>>> lists would be easier:
>>>>
>>>>>>> x = [2,1,3]
>>>>>>> print x.sort()[0]
>>>> 3
>>>>>>> print x
>>>> [2,1,3]

>
>>DB> You already have a way to do what you want:

>
>>>>>> x = [2,1,3]
>>>>>> print sorted(x)[0]

>>DB> 3

>
> What kind of Python produces that?


Assuming you're referring to the latter example, it was added in version 2.4
If you meant the former example, I think that's purely pseudo-Python.

Cheers,
Chris
--
http://blog.rebertia.com
 
Reply With Quote
 
Paul Moore
Guest
Posts: n/a
 
      07-20-2009
2009/7/20 Chris Rebert <>:
> On Mon, Jul 20, 2009 at 2:23 PM, Piet van Oostrum<> wrote:
>>>>>>> x = [2,1,3]
>>>>>>> print sorted(x)[0]
>>>DB> 3

>>
>> What kind of Python produces that?

>
> Assuming you're referring to the latter example, it was added in version 2.4
> If you meant the former example, I think that's purely pseudo-Python.


I think he was referring to the fact that the result should be 1, not 3.

Paul.
 
Reply With Quote
 
Hrvoje Niksic
Guest
Posts: n/a
 
      07-20-2009
Phillip B Oldham <> writes:

> On Jul 20, 6:08*pm, Duncan Booth <duncan.bo...@invalid.invalid> wrote:
>> The main reason why you need both lists and tuples is that because a tuple
>> of immutable objects is itself immutable you can use it as a dictionary
>> key.

>
> Really? That sounds interesting, although I can't think of any real-
> world cases where you'd use something like that.


An application visiting files on a filesystem recursively needs a
dictionary or set keyed by (st_dev, st_ino) to make sure it doesn't
visit the same file twice. Memoization implementation (of a cache for
results of function application) might need to use a dictionary to map
function arguments, a tuple, to the function result.
 
Reply With Quote
 
Hrvoje Niksic
Guest
Posts: n/a
 
      07-20-2009
Chris Rebert <> writes:

>>>>>>> x = [2,1,3]
>>>>>>> print sorted(x)[0]
>>>DB> 3

>>
>> What kind of Python produces that?

>
> Assuming you're referring to the latter example, it was added in version 2.4
> If you meant the former example, I think that's purely pseudo-Python.


sorted([2, 1, 3])[0] evaluates to 1, not 3.
 
Reply With Quote
 
Niels L. Ellegaard
Guest
Posts: n/a
 
      07-20-2009
Phillip B Oldham <> writes:

> We often find we need to do manipulations like the above without
> changing the order of the original list, and languages like JS allow
> this. We can't work out how to do this in python though, other than
> duplicating the list, sorting, reversing, then discarding.


If you just want a one-liner, and you don't care about speed you can
do the following (but I don't think this is considered best practice)

>>> x = [2,1,3]
>>> print list(sorted(x))

[1, 2, 3]
>>> print x

[2, 1, 3]

Niels
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Decisions, Decisions.... Max Burke NZ Computing 25 09-24-2006 05:58 AM
Decisions, decisions... Waterspider Digital Photography 8 12-28-2005 09:48 PM
Decisions Decisions Larry Digital Photography 4 06-28-2005 03:41 PM
File or assembly Crystal.Decisions.Web not found lollypop3_14@yahoo.com ASP .Net 0 11-11-2004 01:31 PM
Architecture Decisions Regarding Common DB Access, Remoting, and Performance JTS ASP .Net 0 01-22-2004 07:51 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57