Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #66611

Re: Why is the interpreter is returning a 'reference'?

From Ned Batchelder <ned@nedbatchelder.com>
Subject Re: Why is the interpreter is returning a 'reference'?
Date 2014-02-17 12:08 -0500
References <9b80c233-ad31-44c8-8a6e-9002ab11bd0d@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.7101.1392656931.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 2/17/14 12:00 PM, Nir wrote:
>>>> k = ['hi','boss']
>>>>
>>>> k
> ['hi', 'boss']
>>>> k= [s.upper for s in k]
>>>> 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'] ?
>
> This isn't a big deal, but I am just curious as to why it does this.
>

You have to invoke s.upper, with parens:

     k = [s.upper() for s in k]

In Python, a function or method is a first-class object, so "s.upper" is 
a reference to the method, "s.upper()" is the result of calling the method.

-- 
Ned Batchelder, http://nedbatchelder.com

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Why is the interpreter is returning a 'reference'? Nir <nirchernia@gmail.com> - 2014-02-17 09:00 -0800
  Re: Why is the interpreter is returning a 'reference'? emile <emile@fenx.com> - 2014-02-17 09:08 -0800
  Re: Why is the interpreter is returning a 'reference'? Joel Goldstick <joel.goldstick@gmail.com> - 2014-02-17 12:08 -0500
  Re: Why is the interpreter is returning a 'reference'? Ned Batchelder <ned@nedbatchelder.com> - 2014-02-17 12:08 -0500
  Re: Why is the interpreter is returning a 'reference'? Marko Rauhamaa <marko@pacujo.net> - 2014-02-17 19:09 +0200
  Re: Why is the interpreter is returning a 'reference'? Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-02-17 11:14 -0600
  Re: Why is the interpreter is returning a 'reference'? John Gordon <gordon@panix.com> - 2014-02-17 19:17 +0000

csiph-web