[Tutor] __getitem__
monikajg at netzero.net
monikajg at netzero.net
Wed Nov 23 07:15:12 EST 2016
Hi:
Thank you very much for your explanation. Just to confirm when using __getitem__ sort will go thru every key in dict and get its value and then sort according to it. Correct?
Also, you wrote:
">>> sorted(numbers, key = lambda ky: numbers[ky]) "
ky is input to lambda. Where does lambda get ky? what is ky value?
Thank you very much
Monika
---------- Original Message ----------
From: Alan Gauld via Tutor <tutor at python.org>
To: tutor at python.org
Subject: Re: [Tutor] __getitem__
Date: Wed, 23 Nov 2016 10:05:50 +0000
On 23/11/16 06:09, monikajg at netzero.net wrote:
> Can you please explain __getitem__?
__getitem__ is the operator overload for indexing.
It is like the __add__() method which overloads the + operator.
So if you imple,ent __add__() in your class you can add two instances
together using + and Python calls your __add__() method behind the scenes.
In the same way when you index a collection object (with []) Python
calls the __getitem__ method behind the scenes. So for a dictionary
you can access the values of the dictionary by indexing it with a key:
d = {1:2,3:4}
n = d[1] # calls d.__getitem__(1) resulting in n = 2
> My understanding is that it brings back dictionary's value.
> Is this correct?
It brings back the value of the provided key.
> If so which value does it bring?
> Does it look up this value by using a key?
Yes.
> Where is this key specified in " numbers.__getitem__" ?
Either by using indexing like numbers[somekey] or by
someone explicitly calling numbers.__getitem__(aKey)
In your example below sorted accesses the values
using the supplied function (which can be any arbitrary
function that accepts an argument and returns a value.)
By providing __getitem__ as the input function sorted
effectively uses the dictionary values.
> The below supposedly brings back dictionary's keys list sorted by values.
> But how does it do it?
By making the sort key the value of each dictionary key in turn
> numbers = {'first': 1, 'second': 2, 'third': 3, 'Fourth': 4}
> sorted(numbers, key=numbers.__getitem__)
If you try it at the prompt:
>>> numbers = {'first': 1, 'second': 2, 'third': 3, 'Fourth': 4}
>>> sorted(numbers, key=numbers.__getitem__)
['first', 'second', 'third', 'Fourth']
You see it works. Now try without getitem:
>>> sorted(numbers)
['Fourth', 'first', 'second', 'third']
This is sorted by the keys. Now lets use a different
sort method to get the values:
def getValue(aKey): return numbers[aKey]
This uses the more familiar indexing technique
to retrieve the value for a given key. We can
now use this function with sorted to get the
original result:
>>> sorted(numbers, key = getValue)
['first', 'second', 'third', 'Fourth']
And we can miss out the separate function definition
by using a lambda:
>>> sorted(numbers, key = lambda ky: numbers[ky])
['first', 'second', 'third', 'Fourth']
>>>
But the authors of your example have used __getitem__
directly because it's already available...(and the indexing
technique calls __getitem__ indirectly anyway).
HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
_______________________________________________
Tutor maillist - Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
____________________________________________________________
Affordable Wireless Plans
Set up is easy. Get online in minutes.
Starting at only $14.95 per month!
www.netzero.net?refcd=nzmem0216
More information about the Tutor
mailing list