Why is the interpreter is returning a 'reference'?
On Mon, Feb 17, 2014 at 11:00 AM, Nir <nirchernia at gmail.com> wrote:
>>>> k = ['hi','boss']
> ['hi', 'boss']
>>>> k= [s.upper for s in k]
> [<built-in method upper of str object at 0x00000000021B2AF8>, <built-in method upper of str object at 0x0000000002283F58>]
> Why doesn't the python interpreter just return
> ['HI, 'BOSS'] ?
It's just doing exactly what you are telling it to :). Your list
comprehension is constructing a list consisting of the 'upper' method
(which are themselves objects, able to be passed around just like any
other value) for each string object in list 'k'. Consider this:
>>> k = ['hi', 'boss']
>>> s = k
>>> s.upper # this just accesses the 'upper' attribute of 's',
which turns out to be its 'upper' method
<built-in method upper of str object at 0xdeadbeef>
>>> s.upper() # this actually calls the 'upper' method on 's'
Change your comprehension to actually call the upper method like so:
"k = [s.upper() for s in k]". It will do what you expected with that
Hope this helps,