Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #66608 > unrolled thread
| Started by | Nir <nirchernia@gmail.com> |
|---|---|
| First post | 2014-02-17 09:00 -0800 |
| Last post | 2014-02-17 19:17 +0000 |
| Articles | 7 — 7 participants |
Back to article view | Back to comp.lang.python
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
| From | Nir <nirchernia@gmail.com> |
|---|---|
| Date | 2014-02-17 09:00 -0800 |
| Subject | Why is the interpreter is returning a 'reference'? |
| Message-ID | <9b80c233-ad31-44c8-8a6e-9002ab11bd0d@googlegroups.com> |
>>> 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.
[toc] | [next] | [standalone]
| From | emile <emile@fenx.com> |
|---|---|
| Date | 2014-02-17 09:08 -0800 |
| Message-ID | <mailman.7102.1392656931.18130.python-list@python.org> |
| In reply to | #66608 |
On 02/17/2014 09:00 AM, Nir wrote: >>>> k = ['hi','boss'] >>>> >>>> k > ['hi', 'boss'] >>>> k= [s.upper for s in k] s.upper is a reference to the method upper of s -- to execute the method add parens -- s.upper() Emile >>>> 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. >
[toc] | [prev] | [next] | [standalone]
| From | Joel Goldstick <joel.goldstick@gmail.com> |
|---|---|
| Date | 2014-02-17 12:08 -0500 |
| Message-ID | <mailman.7100.1392656921.18130.python-list@python.org> |
| In reply to | #66608 |
[Multipart message — attachments visible in raw view] — view raw
On Feb 17, 2014 12:05 PM, "Nir" <nirchernia@gmail.com> wrote: > > >>> k = ['hi','boss'] > >>> > >>> k > ['hi', 'boss'] > >>> k= [s.upper for s in k S.upper() > >>> 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. > -- > https://mail.python.org/mailman/listinfo/python-list
[toc] | [prev] | [next] | [standalone]
| From | Ned Batchelder <ned@nedbatchelder.com> |
|---|---|
| Date | 2014-02-17 12:08 -0500 |
| Message-ID | <mailman.7101.1392656931.18130.python-list@python.org> |
| In reply to | #66608 |
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
[toc] | [prev] | [next] | [standalone]
| From | Marko Rauhamaa <marko@pacujo.net> |
|---|---|
| Date | 2014-02-17 19:09 +0200 |
| Message-ID | <87ob2564yn.fsf@elektro.pacujo.net> |
| In reply to | #66608 |
Nir <nirchernia@gmail.com>: >>>> 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'] ? Try: k = [ s.upper() for s in k ] Marko
[toc] | [prev] | [next] | [standalone]
| From | Zachary Ware <zachary.ware+pylist@gmail.com> |
|---|---|
| Date | 2014-02-17 11:14 -0600 |
| Message-ID | <mailman.7103.1392657278.18130.python-list@python.org> |
| In reply to | #66608 |
On Mon, Feb 17, 2014 at 11:00 AM, Nir <nirchernia@gmail.com> 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'] ? 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[0] >>> s 'hi' >>> 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' 'HI' 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 change. Hope this helps, -- Zach
[toc] | [prev] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2014-02-17 19:17 +0000 |
| Message-ID | <ldtn7j$4v9$2@reader1.panix.com> |
| In reply to | #66608 |
In <9b80c233-ad31-44c8-8a6e-9002ab11bd0d@googlegroups.com> Nir <nirchernia@gmail.com> writes: > >>> 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. Because you typed 'str.upper' instead of 'str.upper()'. -- John Gordon Imagine what it must be like for a real medical doctor to gordon@panix.com watch 'House', or a real serial killer to watch 'Dexter'.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web