Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #67520 > unrolled thread
| Started by | "ast" <nomail@invalid.com> |
|---|---|
| First post | 2014-03-03 10:42 +0100 |
| Last post | 2014-03-05 09:01 +1100 |
| Articles | 20 on this page of 93 — 21 participants |
Back to article view | Back to comp.lang.python
Reference "ast" <nomail@invalid.com> - 2014-03-03 10:42 +0100
Object identity (was: Reference) Ben Finney <ben+python@benfinney.id.au> - 2014-03-03 21:00 +1100
Re: Object identity (was: Reference) "ast" <nomail@invalid.com> - 2014-03-03 11:21 +0100
Re: Reference "Mark H. Harris" <harrismh777@gmail.com> - 2014-03-03 05:09 -0800
Re: Reference Grant Edwards <invalid@invalid.invalid> - 2014-03-03 14:29 +0000
Re: Reference Rustom Mody <rustompmody@gmail.com> - 2014-03-03 07:52 -0800
Re: Reference Ben Finney <ben+python@benfinney.id.au> - 2014-03-04 08:10 +1100
Re: Reference Tim Chase <python.list@tim.thechases.com> - 2014-03-03 15:24 -0600
Re: Reference Ben Finney <ben+python@benfinney.id.au> - 2014-03-04 08:31 +1100
Re: Reference Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-03-03 21:35 +0000
Re: Reference Marko Rauhamaa <marko@pacujo.net> - 2014-03-04 00:07 +0200
Re: Reference Ben Finney <ben+python@benfinney.id.au> - 2014-03-04 09:18 +1100
Re: Reference Alister <alister.ware@ntlworld.com> - 2014-03-04 11:10 +0000
Re: Reference Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-04 11:48 +0000
Re: Reference "Rhodri James" <rhodri@wildebst.org.uk> - 2014-03-05 00:25 +0000
Re: Reference Tim Chase <python.list@tim.thechases.com> - 2014-03-03 15:51 -0600
Re: Reference Jerry Hill <malaclypse2@gmail.com> - 2014-03-03 17:02 -0500
Re: Reference Marko Rauhamaa <marko@pacujo.net> - 2014-03-04 00:22 +0200
Re: Reference Chris Angelico <rosuav@gmail.com> - 2014-03-04 09:27 +1100
Re: Reference Ben Finney <ben+python@benfinney.id.au> - 2014-03-04 09:33 +1100
Re: Reference Steven D'Aprano <steve@pearwood.info> - 2014-03-04 04:52 +0000
Re: Reference Chris Angelico <rosuav@gmail.com> - 2014-03-04 16:24 +1100
Re: Reference "Rhodri James" <rhodri@wildebst.org.uk> - 2014-03-05 01:08 +0000
Re: Reference Roy Smith <roy@panix.com> - 2014-03-04 21:09 -0500
Re: Reference Rustom Mody <rustompmody@gmail.com> - 2014-03-04 19:36 -0800
Re: Reference Ian Kelly <ian.g.kelly@gmail.com> - 2014-03-04 21:08 -0700
Re: Reference Rustom Mody <rustompmody@gmail.com> - 2014-03-04 20:31 -0800
Re: Reference Ben Finney <ben+python@benfinney.id.au> - 2014-03-05 15:32 +1100
Re: Reference Rustom Mody <rustompmody@gmail.com> - 2014-03-04 20:47 -0800
Re: Reference Steven D'Aprano <steve@pearwood.info> - 2014-03-05 05:06 +0000
Re: Reference Rustom Mody <rustompmody@gmail.com> - 2014-03-04 21:47 -0800
Re: Reference alex23 <wuwei23@gmail.com> - 2014-03-05 16:01 +1000
Re: Reference Rustom Mody <rustompmody@gmail.com> - 2014-03-04 22:10 -0800
Re: Reference Ben Finney <ben+python@benfinney.id.au> - 2014-03-05 17:22 +1100
Re: Reference alex23 <wuwei23@gmail.com> - 2014-03-05 16:28 +1000
Re: Reference Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-03-05 12:24 +0000
Re: Reference Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-03-05 12:21 +0000
Re: Reference Ben Finney <ben+python@benfinney.id.au> - 2014-03-05 17:20 +1100
Re: Reference Rustom Mody <rustompmody@gmail.com> - 2014-03-05 09:40 -0800
Re: Reference Tim Chase <python.list@tim.thechases.com> - 2014-03-05 12:12 -0600
Re: Reference Ben Finney <ben+python@benfinney.id.au> - 2014-03-06 05:33 +1100
Re: Reference Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-05 18:19 +0000
Re: Reference Marko Rauhamaa <marko@pacujo.net> - 2014-03-05 22:23 +0200
Re: Reference Grant Edwards <invalid@invalid.invalid> - 2014-03-05 20:31 +0000
Re: Reference Marko Rauhamaa <marko@pacujo.net> - 2014-03-05 22:46 +0200
Re: Reference Ben Finney <ben+python@benfinney.id.au> - 2014-03-06 08:07 +1100
Re: Reference Ben Finney <ben+python@benfinney.id.au> - 2014-03-06 08:10 +1100
Re: Reference Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-03-05 21:34 +0000
Re: Reference Terry Reedy <tjreedy@udel.edu> - 2014-03-05 18:00 -0500
Re: Reference Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-06 03:01 +0000
Re: Reference Rustom Mody <rustompmody@gmail.com> - 2014-03-04 22:03 -0800
Re: Reference Ben Finney <ben+python@benfinney.id.au> - 2014-03-05 17:26 +1100
Re: Reference Chris Angelico <rosuav@gmail.com> - 2014-03-05 17:32 +1100
Re: Reference Tim Chase <python.list@tim.thechases.com> - 2014-03-05 08:24 -0600
Re: Reference Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-05 18:29 +0000
Re: Reference Marko Rauhamaa <marko@pacujo.net> - 2014-03-05 22:34 +0200
Re: Reference Ben Finney <ben+python@benfinney.id.au> - 2014-03-06 08:01 +1100
Re: Reference Marko Rauhamaa <marko@pacujo.net> - 2014-03-05 23:14 +0200
Re: Reference Chris Angelico <rosuav@gmail.com> - 2014-03-06 08:26 +1100
Re: Reference Marko Rauhamaa <marko@pacujo.net> - 2014-03-05 23:50 +0200
Re: Reference Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-06 00:35 +0000
Re: Reference Chris Angelico <rosuav@gmail.com> - 2014-03-06 11:50 +1100
Re: Reference Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-03-06 17:46 +0000
Re: Reference Tim Chase <python.list@tim.thechases.com> - 2014-03-05 15:33 -0600
Re: Reference Ben Finney <ben+python@benfinney.id.au> - 2014-03-06 08:37 +1100
Re: Reference Marko Rauhamaa <marko@pacujo.net> - 2014-03-06 02:52 +0200
Re: Reference Ben Finney <ben+python@benfinney.id.au> - 2014-03-06 12:05 +1100
Re: Reference alex23 <wuwei23@gmail.com> - 2014-03-06 12:12 +1000
Re: Reference Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-03-05 21:46 +0000
Re: Reference Marko Rauhamaa <marko@pacujo.net> - 2014-03-05 08:23 +0200
Re: Reference Rustom Mody <rustompmody@gmail.com> - 2014-03-04 22:33 -0800
Re: Reference Ben Finney <ben+python@benfinney.id.au> - 2014-03-05 17:40 +1100
Re: Reference Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-03-05 12:35 +0000
Re: Reference Chris Angelico <rosuav@gmail.com> - 2014-03-05 23:45 +1100
Re: Reference Jerry Hill <malaclypse2@gmail.com> - 2014-03-04 10:19 -0500
Re: Reference Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-04 15:42 +0000
Re: Reference Chris Angelico <rosuav@gmail.com> - 2014-03-05 03:02 +1100
Re: Reference Roy Smith <roy@panix.com> - 2014-03-04 11:14 -0500
Re: Reference MRAB <python@mrabarnett.plus.com> - 2014-03-04 17:12 +0000
Re: Reference Rustom Mody <rustompmody@gmail.com> - 2014-03-04 08:24 -0800
Re: Reference Chris Angelico <rosuav@gmail.com> - 2014-03-05 02:25 +1100
Re: Reference Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2014-03-05 14:37 +0100
Re: Reference Steven D'Aprano <steve@pearwood.info> - 2014-03-04 03:59 +0000
Re: Reference Ben Finney <ben+python@benfinney.id.au> - 2014-03-04 09:17 +1100
Re: Reference Roy Smith <roy@panix.com> - 2014-03-03 18:02 -0500
Re: Reference Chris Angelico <rosuav@gmail.com> - 2014-03-04 10:09 +1100
Re: Reference Steven D'Aprano <steve@pearwood.info> - 2014-03-04 04:38 +0000
Re: Reference Terry Reedy <tjreedy@udel.edu> - 2014-03-03 13:48 -0500
Re: Reference Steven D'Aprano <steve@pearwood.info> - 2014-03-04 03:45 +0000
Re: Reference Alexander Blinne <news@blinne.net> - 2014-03-04 13:55 +0100
Re: Reference Chris Angelico <rosuav@gmail.com> - 2014-03-05 01:06 +1100
Re: Reference Alexander Blinne <news@blinne.net> - 2014-03-04 22:53 +0100
Re: Reference Chris Angelico <rosuav@gmail.com> - 2014-03-05 09:01 +1100
Page 1 of 5 [1] 2 3 4 5 Next page →
| From | "ast" <nomail@invalid.com> |
|---|---|
| Date | 2014-03-03 10:42 +0100 |
| Subject | Reference |
| Message-ID | <53144e8d$0$2149$426a74cc@news.free.fr> |
hello Consider following code: >>> A=7 >>> B=7 >>> A is B True I understand that there is a single object 7 somewhere in memory and both variables A and B point toward this object 7 now do the same with a list: >>> l1 = [1, 2] >>> l2 = [1, 2] >>> l1 is l2 False It seems this time that there are 2 distincts objects [1, 2] in memory. l1 points toward the first one and l2 points toward the second one. if I change one, the second remains unchanged >>> l1.append(3) >>> l1 [1, 2, 3] >>> l2 [1, 2] I dont really understand why the behavior is different. Both integer 7 and list [1, 2] are objects. Why is it different ? thanks
[toc] | [next] | [standalone]
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Date | 2014-03-03 21:00 +1100 |
| Subject | Object identity (was: Reference) |
| Message-ID | <mailman.7632.1393840833.18130.python-list@python.org> |
| In reply to | #67520 |
"ast" <nomail@invalid.com> writes: > >>> A=7 > >>> B=7 > >>> A is B > True > > I understand that there is a single object 7 somewhere in memory and > both variables A and B point toward this object 7 Try not to think in terms of “point to”. Rather, the names “A” and “B” are bound to that object. The distinction is subtle; but it's important to realise that *all* references in Python do this, and there's no way to talk about an object in Python without using a reference. The “pointer” model from other languages doesn't exist in Python. > now do the same with a list: > > >>> l1 = [1, 2] > >>> l2 = [1, 2] > >>> l1 is l2 > False > > It seems this time that there are 2 distincts objects [1, 2] in > memory. That's correct. > I dont really understand why the behavior is different. Both integer 7 > and list [1, 2] are objects. Why is it different ? Short answer: object identity is an implementation detail. It's up to the Python implementation to decide when to re-use an object when a new one is requested. No guarantee is made, when you ask to create an object, that you won't get an existing one if that would work just as well. Since the integer object 7 is immutable, it will behave the same no matter how many times you ask for a new one, the Python implementation can choose to give you the same object. But it might not — don't depend on this! Since two separate lists are mutable, each one can have a distinct history after creation, so it would be less helpful to return an existing list when you ask for a new one. But again, there's no guarantee here either! A Python implementation might decide to give you an existing list, if existing guarantees can be kept. The moral is: Don't depend on differences in object identity. You can be guaranteed that an object will retain its own identity, and its identity will always be different from all other co-existing objects that have different values. Beyond that, don't make any assumptions. -- \ “Programs must be written for people to read, and only | `\ incidentally for machines to execute.” —Abelson & Sussman, | _o__) _Structure and Interpretation of Computer Programs_ | Ben Finney
[toc] | [prev] | [next] | [standalone]
| From | "ast" <nomail@invalid.com> |
|---|---|
| Date | 2014-03-03 11:21 +0100 |
| Subject | Re: Object identity (was: Reference) |
| Message-ID | <531457cd$0$2032$426a74cc@news.free.fr> |
| In reply to | #67522 |
thanks ben, that's clear
[toc] | [prev] | [next] | [standalone]
| From | "Mark H. Harris" <harrismh777@gmail.com> |
|---|---|
| Date | 2014-03-03 05:09 -0800 |
| Message-ID | <ed7cc56a-6988-4f8e-9a95-f5bbfa1dbc4a@googlegroups.com> |
| In reply to | #67520 |
On Monday, March 3, 2014 3:42:30 AM UTC-6, ast wrote: > Consider following code: > > >>> A=7 > >>> B=7 > >>> A is B > True The names A and B are both bound to the same object (7). You will discover that this is True for all small ints less than 257; on CPython3.3.4. I just checked it. :) Its just more efficient to do this for small ints because they are immutable (1) and because they are used for frequently (2). As somebody pointed out last week, don't use "is" for ints and strings (generally speaking). Value is what's important here, not identity. My python coding is yet simple and straight forward enough that I have not had a need yet for "is". Cheers marcus
[toc] | [prev] | [next] | [standalone]
| From | Grant Edwards <invalid@invalid.invalid> |
|---|---|
| Date | 2014-03-03 14:29 +0000 |
| Message-ID | <lf23ki$ag5$2@reader1.panix.com> |
| In reply to | #67520 |
On 2014-03-03, ast <nomail@invalid.com> wrote:
> hello
>
> Consider following code:
>
>>>> A=7
>>>> B=7
>>>> A is B
> True
>
> I understand that there is a single object 7 somewhere in memory
Maybe, maybe not. Integer are immutable, so that's allowed but not
required. In CPython, that's true for small integers, but that is an
implementation detail, and you shouldn't depend on it.
> and both variables A and B point toward this object 7
They might. They might not.
> now do the same with a list:
>
>>>> l1 = [1, 2]
>>>> l2 = [1, 2]
>>>> l1 is l2
> False
>
> It seems this time that there are 2 distincts objects [1, 2] in
> memory.
Yep. Lists are mutable, therefore each literal produces a distinct
object.
> l1 points toward the first one and l2 points toward the
> second one.
Yep.
> I dont really understand why the behavior is different.
> Both integer 7 and list [1, 2] are objects. Why is it
> different ?
Integer objects are immutable (they can't change value), therefore you
can reuse them without causing problems. Lists are mutable (you can
change the values in them), so you can't reuse them.
--
Grant Edwards grant.b.edwards Yow! Maybe I should have
at asked for my Neutron Bomb
gmail.com in PAISLEY --
[toc] | [prev] | [next] | [standalone]
| From | Rustom Mody <rustompmody@gmail.com> |
|---|---|
| Date | 2014-03-03 07:52 -0800 |
| Message-ID | <1d1dfa1b-b715-4d8f-9c12-f0d3dc1a22c9@googlegroups.com> |
| In reply to | #67520 |
On Monday, March 3, 2014 3:12:30 PM UTC+5:30, ast wrote: > hello > Consider following code: > >>> A=7 > >>> B=7 > >>> A is B > True > I understand that there is a single object 7 somewhere in memory and > both variables A and B point toward this object 7 > now do the same with a list: > >>> l1 = [1, 2] > >>> l2 = [1, 2] > >>> l1 is l2 > False > It seems this time that there are 2 distincts objects [1, 2] in memory. l1 points > toward the first one and l2 points toward the second one. > if I change one, the second remains unchanged > >>> l1.append(3) > >>> l1 > [1, 2, 3] > >>> l2 > [1, 2] > I dont really understand why the behavior is different. > Both integer 7 and list [1, 2] are objects. Why is it > different ? Short answer: Avoid using 'is'. Long answer: http://www.beyondwilber.ca/healing-thinking/non-identity-korzybski.html Pragmatic answer: Think of 'is' as a short-form for 'machine-rep-is' And use machine representations with the same alacrity that a C programmer uses inline assembly
[toc] | [prev] | [next] | [standalone]
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Date | 2014-03-04 08:10 +1100 |
| Message-ID | <mailman.7659.1393881306.18130.python-list@python.org> |
| In reply to | #67561 |
Rustom Mody <rustompmody@gmail.com> writes: > Short answer: Avoid using 'is'. This is bad advice in a Python forum. The ‘is’ operator is commonly used in Python, so please don't advise against it in an unqualified “short answer”. > Long answer: http://www.beyondwilber.ca/healing-thinking/non-identity-korzybski.html Interesting, but mostly a distraction for the querent here. Short answer: Use ‘use’ any time you need to compare object identity. You usually do not need to compare object identity. -- \ “Some people, when confronted with a problem, think ‘I know, | `\ I'll use regular expressions’. Now they have two problems.” | _o__) —Jamie Zawinski, in alt.religion.emacs | Ben Finney
[toc] | [prev] | [next] | [standalone]
| From | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| Date | 2014-03-03 15:24 -0600 |
| Message-ID | <mailman.7660.1393881859.18130.python-list@python.org> |
| In reply to | #67561 |
On 2014-03-04 08:10, Ben Finney wrote: > > Long answer: > > http://www.beyondwilber.ca/healing-thinking/non-identity-korzybski.html > > Interesting, but mostly a distraction for the querent here. > > Short answer: Use ‘use’ any time you need to compare object > identity. You usually do not need to compare object identity. I think there use something wrong with that sentence...unless there usen't. ;-) -tkc
[toc] | [prev] | [next] | [standalone]
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Date | 2014-03-04 08:31 +1100 |
| Message-ID | <mailman.7661.1393882299.18130.python-list@python.org> |
| In reply to | #67561 |
Tim Chase <python.list@tim.thechases.com> writes: > On 2014-03-04 08:10, Ben Finney wrote: > > Short answer: Use ‘use’ any time you need to compare object > > identity. You usually do not need to compare object identity. Damn it, a snappy response marred by a typo. > I think there use something wrong with that sentence...unless there > usen't. ;-) That use correct. Thanks, Tim :-) -- \ “Software patents provide one more means of controlling access | `\ to information. They are the tool of choice for the internet | _o__) highwayman.” —Anthony Taylor | Ben Finney
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2014-03-03 21:35 +0000 |
| Message-ID | <mailman.7663.1393882574.18130.python-list@python.org> |
| In reply to | #67561 |
On 03/03/2014 21:10, Ben Finney wrote: > Rustom Mody <rustompmody@gmail.com> writes: > >> Short answer: Avoid using 'is'. > > This is bad advice in a Python forum. > > The ‘is’ operator is commonly used in Python, so please don't advise > against it in an unqualified “short answer”. > >> Long answer: http://www.beyondwilber.ca/healing-thinking/non-identity-korzybski.html > > Interesting, but mostly a distraction for the querent here. > > Short answer: Use ‘use’ any time you need to compare object identity. > You usually do not need to compare object identity. > That last paragraph is as clear as mud. I'd just like to know why people are so obsessed with identities, I've never thought to use them in 10+ years of writing Python. Do I use the KISS principle too often? -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com
[toc] | [prev] | [next] | [standalone]
| From | Marko Rauhamaa <marko@pacujo.net> |
|---|---|
| Date | 2014-03-04 00:07 +0200 |
| Message-ID | <87wqgb6imm.fsf@elektro.pacujo.net> |
| In reply to | #67587 |
Mark Lawrence <breamoreboy@yahoo.co.uk>: > I'd just like to know why people are so obsessed with identities, I've > never thought to use them in 10+ years of writing Python. Do I use the > KISS principle too often? Calmly choosing the right tool for the job is not an obsession. Marko
[toc] | [prev] | [next] | [standalone]
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Date | 2014-03-04 09:18 +1100 |
| Message-ID | <mailman.7671.1393885206.18130.python-list@python.org> |
| In reply to | #67595 |
Marko Rauhamaa <marko@pacujo.net> writes: > Mark Lawrence <breamoreboy@yahoo.co.uk>: > > > I'd just like to know why people are so obsessed with identities, I've > > never thought to use them in 10+ years of writing Python. Do I use the > > KISS principle too often? > > Calmly choosing the right tool for the job is not an obsession. Persistently banging your head in contradiction to the facts of Python's data model, as you have been doing, starts to look very much like obsession. -- \ “Please to bathe inside the tub.” —hotel room, Japan | `\ | _o__) | Ben Finney
[toc] | [prev] | [next] | [standalone]
| From | Alister <alister.ware@ntlworld.com> |
|---|---|
| Date | 2014-03-04 11:10 +0000 |
| Message-ID | <KwiRu.19$Cd2.6@fx19.am4> |
| In reply to | #67602 |
On Tue, 04 Mar 2014 09:18:57 +1100, Ben Finney wrote: > Marko Rauhamaa <marko@pacujo.net> writes: > >> Mark Lawrence <breamoreboy@yahoo.co.uk>: >> >> > I'd just like to know why people are so obsessed with identities, >> > I've never thought to use them in 10+ years of writing Python. Do I >> > use the KISS principle too often? >> >> Calmly choosing the right tool for the job is not an obsession. > > Persistently banging your head in contradiction to the facts of Python's > data model, as you have been doing, starts to look very much like > obsession. Definition of insanity Doing the same thing over and over again & expecting different results -- Support staff hung over, send aspirin and come back LATER.
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2014-03-04 11:48 +0000 |
| Message-ID | <5315bd9e$0$29985$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #67661 |
On Tue, 04 Mar 2014 11:10:34 +0000, Alister wrote: > Definition of insanity > > Doing the same thing over and over again & expecting different results *rolls dice* :-) -- Steven D'Aprano http://import-that.dreamwidth.org/
[toc] | [prev] | [next] | [standalone]
| From | "Rhodri James" <rhodri@wildebst.org.uk> |
|---|---|
| Date | 2014-03-05 00:25 +0000 |
| Message-ID | <op.xb73gypp5079vu@gnudebeest> |
| In reply to | #67665 |
On Tue, 04 Mar 2014 11:48:46 -0000, Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote: > On Tue, 04 Mar 2014 11:10:34 +0000, Alister wrote: > >> Definition of insanity >> >> Doing the same thing over and over again & expecting different results > > *rolls dice* As any gamer will tell you, dice are not random number generators. They are far too cussed for that :-) -- Rhodri James *-* Wildebeest Herder to the Masses
[toc] | [prev] | [next] | [standalone]
| From | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| Date | 2014-03-03 15:51 -0600 |
| Message-ID | <mailman.7664.1393883448.18130.python-list@python.org> |
| In reply to | #67561 |
On 2014-03-03 21:35, Mark Lawrence wrote:
> I'd just like to know why people are so obsessed with identities,
> I've never thought to use them in 10+ years of writing Python. Do
> I use the KISS principle too often?
There are a couple use-cases I've encountered where "is" matters:
1) the most popular:
if foo is None:
do_stuff()
2) sentinels (which are effectively non-None None values)
SENTINEL = object()
def myfuntion(value=SENTINEL):
if value is SENTINEL:
do_something_parameterless()
else:
do_something_else(value) # allow for value=None
3) when doing recursion and you want to prevent touching the same
object multiple times, such as what happens when you do
lst = [1,2,3]
lst.append(lst)
print(lst)
and it needs to recognize that the final element of "lst" is one that
it has already seen (as done by identity).
There might be some other use cases, but #1 is a good 95% of my
usage, #2 is a good 4%, and I can only think of once in my Python
career when I've needed to do what is described in #3.
-tkc
[toc] | [prev] | [next] | [standalone]
| From | Jerry Hill <malaclypse2@gmail.com> |
|---|---|
| Date | 2014-03-03 17:02 -0500 |
| Message-ID | <mailman.7666.1393884138.18130.python-list@python.org> |
| In reply to | #67561 |
On Mon, Mar 3, 2014 at 4:51 PM, Tim Chase <python.list@tim.thechases.com> wrote:
> There are a couple use-cases I've encountered where "is" matters:
>
> 1) the most popular:
>
> if foo is None:
> do_stuff()
I know this is the one that always comes up, but honestly, I feel like
"is" doesn't matter here. That code would be just as correct if it
was written as:
if foo == None:
do_stuff()
The only time it would give you a different result from the "is"
version is if foo was bound to an object that returned True when
compared with None. And if that were the case, I'm still not
convinced that you can tell from looking at those two lines of code
which one is buggy, except for the fact that there has been 20 years
of custom saying that comparing to None with equality is wrong.
--
Jerry
[toc] | [prev] | [next] | [standalone]
| From | Marko Rauhamaa <marko@pacujo.net> |
|---|---|
| Date | 2014-03-04 00:22 +0200 |
| Message-ID | <87siqy7whs.fsf@elektro.pacujo.net> |
| In reply to | #67593 |
Jerry Hill <malaclypse2@gmail.com>: > except for the fact that there has been 20 years of custom saying that > comparing to None with equality is wrong. "if foo == None" is not wrong in any manner. It's just that if you are comfortable with the "is" operator and its semantics, "if foo is None" is slightly more natural. You generally use "==" if more than one object could be equal. If you know there's only one object of the kind, you convey that knowledge by the use of "is" even when functionally, it doesn't matter. Marko
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-03-04 09:27 +1100 |
| Message-ID | <mailman.7672.1393885656.18130.python-list@python.org> |
| In reply to | #67603 |
On Tue, Mar 4, 2014 at 9:22 AM, Marko Rauhamaa <marko@pacujo.net> wrote: > You generally use "==" if more than one object could be equal. If you > know there's only one object of the kind, you convey that knowledge by > the use of "is" even when functionally, it doesn't matter. It's even simpler than that. You use "==" when you care about value, and "is" when you care about identity. This is because "==" tests value, and "is" tests identity. I do not understand why there is confusion. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Date | 2014-03-04 09:33 +1100 |
| Message-ID | <mailman.7675.1393886106.18130.python-list@python.org> |
| In reply to | #67603 |
Marko Rauhamaa <marko@pacujo.net> writes: > Jerry Hill <malaclypse2@gmail.com>: > > > except for the fact that there has been 20 years of custom saying that > > comparing to None with equality is wrong. > > "if foo == None" is not wrong in any manner. Marko, please don't keep asserting falsehoods. It's already been pointed out in this forum many times, essentially since the forum existed, why what you say here is false. -- \ “The fact that I have no remedy for all the sorrows of the | `\ world is no reason for my accepting yours. It simply supports | _o__) the strong probability that yours is a fake.” —Henry L. Mencken | Ben Finney
[toc] | [prev] | [next] | [standalone]
Page 1 of 5 [1] 2 3 4 5 Next page →
Back to top | Article view | comp.lang.python
csiph-web