Still trying to understand "=="... It appears as if two equal objects
can become unequal if you perform the same operations on them: l=[1] s=l l.append(s) w=[1] r=[1,w] w.append(r) s
[1, [...]] w
[1, [1, [...]]] s==w
True
Note that they're equal, yet are printed differently.
s[0]=2 w[0]=2 s==w
False
All of a sudden they have become unequal.
Axel 26 1883
"Axel Boldt" <ax*******@yaho o.com> wrote in message
news:40******** *************** ***@posting.goo gle.com... Still trying to understand "=="... It appears as if two equal objects can become unequal if you perform the same operations on them:
>>> l=[1] >>> s=l >>> l.append(s) >>> w=[1] >>> r=[1,w] >>> w.append(r) >>> s [1, [...]] >>> w [1, [1, [...]]] >>> s==w True
Note that they're equal, yet are printed differently. >>> s[0]=2 >>> w[0]=2 >>> s==w
False
All of a sudden they have become unequal.
Axel
You've got a recursive structure! I originally thought
that the [...] was something you'd done to the printout,
but it isn't.
I think the original True is a bug. It's getting confused
by the recursion.
John Roth
Axel Boldt wrote: Still trying to understand "=="... It appears as if two equal objects can become unequal if you perform the same operations on them:
>>> l=[1] >>> s=l >>> l.append(s) >>> w=[1] >>> r=[1,w] >>> w.append(r) >>> s [1, [...]] >>> w [1, [1, [...]]] >>> s==w True
You're creating recursive lists (which is extremely rare in the real
world; I've never seen anyone create a self-referencing list in
real-life code. Tree structures and even recursive data structures are
one thing, but you're considering a very weird corner case here.
Note that they're equal, yet are printed differently.
That's fine. 1 and 1.0 are printed differently (and even are of
different types) but they're equal. If you try running through the
element-wise algorithm that Python uses to determine equality of lists,
you'll see that they are in fact equal at this point. The first element
is 1, the second element is a list whose first element is 1, the second
element of that list is a list whose first element is 1, ad infinitum. >>> s[0]=2 >>> w[0]=2 >>> s==w False
All of a sudden they have become unequal.
Because now they really are not equal. Show them: s
[2, [...]] w
[2, [1, [...]]]
The [...] doesn't indicate which sublist is recursively contained, so
you have to dig deeper:
s[1]
[2, [...]] w[1][1]
[2, [1, [...]]]
So s is
[2, [2, [2, [2, ...]]]]
but w is
[2, [1, [2, [1, ...]]]]
Despite both recursing, it's clear that these lists are not element-wise
equal, and so Python is completely right in saying they're not.
Again, though, real-world creation of self-referencing lists is
extremely rare at best.
--
__ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
\__/ Here day fights with night.
-- (the last words of Victor Hugo)
John Roth wrote: "Axel Boldt" <ax*******@yaho o.com> wrote in message news:40******** *************** ***@posting.goo gle.com... Still trying to understand "=="... It appears as if two equal objects can become unequal if you perform the same operations on them:
>>> l=[1] >>> s=l >>> l.append(s) >>> w=[1] >>> r=[1,w] >>> w.append(r) >>> s [1, [...]] >>> w [1, [1, [...]]] >>> s==w True
Note that they're equal, yet are printed differently.
>>> s[0]=2 >>> w[0]=2 >>> s==w False
All of a sudden they have become unequal.
Axel
You've got a recursive structure! I originally thought that the [...] was something you'd done to the printout, but it isn't.
I think the original True is a bug. It's getting confused by the recursion.
Nah, Python's completely right here.
That the original s and w have extra finite elements doesn't make them
unequal. Follow Python's elementwise equality algorithm down and you'll
see they must be equal. The original s is l = [1] s = l l.append(s) s
[1, [...]]
so it is
[1, [1, [1, [1, [1, ...]]]]]
The original w is
w = [1] r = [1, w] w.append(r) w
[1, [1, [...]]]
so it is also
[1, [1, [1, [1, [1, ...]]]]]
The real weirdness you reach is when the recursive sublists are not
appended, but prepended:
a = [1] a.insert(0, a) a
[[...], 1]
Here you could never even resolve a first element for comparison!
--
__ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
\__/ Here day fights with night.
-- (the last words of Victor Hugo)
John Roth wrote: >>> l=[1] >>> s=l >>> l.append(s) >>> w=[1] >>> r=[1,w] >>> w.append(r) >>> s [1, [...]] >>> w [1, [1, [...]]] >>> s==w True
You've created two cyclic data structures. The ==
operator happens to regard them as equal, but they're
not identical, since they have different structures.
The first one is a list which refers directly to
itself, and the second contains another list which
refers back to the first list. >>> s[0]=2 >>> w[0]=2 >>> s==w False
If you print out s and w at this point: s
[2, [...]] w
[2, [1, [...]]]
They're now different in a way that == can see, so
it says they're not equal.
It appears as if two equal objects can become unequal if you perform the same operations on them
Since the objects weren't really the same to begin with,
performing "the same operation" on them isn't entirely
possible.
By the way, it's not unusual for two objects that are not
identical to nevertheless compare ==, e.g.
5 == 5.0
True
even though one is an integer and the other is a float.
Generally, == means that the *value* of two objects is
"equivalent " in some sense. What sense that is depends
on the types of the objects.
The == machinery for lists happens to include some
heuristics which attempt to make sense of cyclic
structures. As you've seen, however, the results can be
a bit confusing, and mightn't be what you want in some
cases. Probably it's best to avoid applying == to cyclic
structures if you can.
--
Greg Ewing, Computer Science Dept,
University of Canterbury,
Christchurch, New Zealand http://www.cosc.canterbury.ac.nz/~greg
At some point, ax*******@yahoo .com (Axel Boldt) wrote: Still trying to understand "=="... It appears as if two equal objects can become unequal if you perform the same operations on them:
>>> l=[1] >>> s=l >>> l.append(s)
You're building a cyclic list: l contains a reference to itself.
General advice: don't do that unless you know what you're doing.
>>> w=[1] >>> r=[1,w] >>> w.append(r) >>> s [1, [...]] >>> w [1, [1, [...]]] >>> s==w True
Note that they're equal, yet are printed differently.
Element-by-element, they're equal, yes. >>> s[0]=2 >>> w[0]=2 >>> s==w
False
All of a sudden they have become unequal.
That's b/c s originally looked like this if we expand out the
recursion a few more levels:
[1, [1, [1, [1, [...]]]]]
where each sub-list is also s.
w also looks like
[1, [1, [1, [1, [...]]]]]
where the *second* sub-list is w.
Set the first elements to 2:
s is now:
[2, [2, [2, [2, ...]]]]
w is now:
[2, [1, [2, [1, ...]]]]
See? The second sub-list of w is not w, so it wasn't changed.
--
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)phy sics(dot)mcmast er(dot)ca
On Tue, Mar 16, 2004 at 07:07:27PM -0500, John Roth wrote: "Axel Boldt" <ax*******@yaho o.com> wrote in message news:40******** *************** ***@posting.goo gle.com...
[...] >>> s [1, [...]] >>> w [1, [1, [...]]] >>> s==w True
[...] I think the original True is a bug. It's getting confused by the recursion.
I don't think it's a bug. In what sense are they unequal? Both variables
are a sequence of two elements, the integer 1, and a sequence that's equal
to the outer sequence (i.e. s==s[1] is True, as Python will tell you).
Every element of the sequence s is equal to the corresponding element of the
sequence w, as far as I can see, even though there is infinite recursion.
Python is behaving exactly as I would expect it to. (Although in the face
of such pathological data structures, I wouldn't mind terribly much if it
didn't...)
-Andrew.
"Axel Boldt" <ax*******@yaho o.com> wrote in message
news:40******** *************** ***@posting.goo gle.com... Still trying to understand "=="...
means equal in value as defined be Python and user code
It appears as if two equal objects can become unequal if you perform the same operations on them:
>>> l=[1] >>> s=l >>> l.append(s) >>> w=[1] >>> r=[1,w] >>> w.append(r) >>> s [1, [...]] >>> w [1, [1, [...]]] >>> s==w True
Note that they're equal, yet are printed differently.
This is routine: print 0, 0.0, 0==0.0
0 0.0 1 1.0 == .99999999999999 999999999999999 9999999
1
Equal in value != equal in internal structure.
>>> s[0]=2 >>> w[0]=2 >>> s==w False
All of a sudden they have become unequal.
1 == 1.0 but type(1) != type(1.0). Okay, that's cheating, so 1|1
1 1.0|1
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unsupported operand type(s) for |: 'float' and 'int'
Doing same thing to equals has different results. Or
a=1234567890987 6 b=float(a) a*a, b*b
(15241578774575 7059730335376L, 1.5241578774575 706e+026)
now unequal to human eye, but
a*a == b*b
1
due to conversion of a*a to float(a*a) for comparison.
Terry J. Reedy
Erik Max Francis wrote: You're creating recursive lists (which is extremely rare in the real world; I've never seen anyone create a self-referencing list in real-life code. Tree structures and even recursive data structures are one thing, but you're considering a very weird corner case here.
Actually they are not that seldom. I have made several. But off course
when I got my recursive code debugged they where gone
;-)
regards Max M
Erik Max Francis <ma*@alcyone.co m> wrote Axel Boldt wrote:
>>> l=[1] >>> s=l >>> l.append(s) >>> w=[1] >>> r=[1,w] >>> w.append(r) >>> s [1, [...]] >>> w [1, [1, [...]]] >>> s==w True
If you try running through the element-wise algorithm that Python uses to determine equality of lists, you'll see that they are in fact equal at this point.
I couldn't find that algorithm in the language reference, only in 5.9:
"Tuples and lists are compared lexicographical ly using comparison of
corresponding elements. This means that to compare equal, each element
must compare equal and the two sequences must be of the same type and
have the same length."
That definition doesn't seem to fully specify equality of list values:
to determine whether s==w, I first note that they both have length 2
and both have 1 as their first element, the second element of s is a
pointer to s, the second element of w is a two-element list, with
first element 1 and second element a pointer to w. So now I have to
determine whether s is equal to the two element list with first
element 1 and second element a pointer to w. They both have two
elements, they both share the first element 1, the second element of s
is a pointer to s, the second element of the other list is a pointer
to w. Now I'm back where I started: I have to test whether s==w. So
the definition in the language reference is circular and both
conclusions s==w and s!=w are consistent with it. I hope that the
actual result received, i.e. s==w, is not implementation dependent?
It now appears to me that there are four levels of equality in Python:
a is b => pickle.dumps(a) ==pickle.dumps( b) => repr(a)==repr(b ) => a==b
and none of the arrows can be reversed in general. I suppose I had
naively assumed the last three to be equivalent. Is there a string
function analogous to pickle.dumps or repr which only takes values
into account, not internal structure? Such a function could be useful
for hashing the values of mutables.
Axel This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Stefan Höhne |
last post by:
Hi,
as I recon, std::vector::clear()'s semantics changed from MS VC++ 6.0 to
MS' DOT.NET - compiler.
In the 6.0 version the capacity() of the vector did not change with the
call to clear(), in DOT.NET the capacity() is reduced to 0.
|
by: Karl Smith |
last post by:
Headless <invalid_address@dna.ie> wrote in reply to Christoph Paeper
<crissov@gmx.net>:
> Semantically <span>this</span> is 100% identical to:
> Semantically <div>this</div> is 100% identical to:
> Semantically this is 100% identical to:
Ah yes, "semantics," the false god of the CIWAHians and other kooks
who don't understand that one marks up *structure*.
|
by: David Rasmussen |
last post by:
The problem comes up in all sorts of contexts. But here is an example:
We want to model the basics of chess, maybe for making a chess program,
or maybe just to make an interactive board or ...
We have to have the notions of colors, squares and pieces represented.
Ideally, we want to be able to do things like initializing the board:
void initial()
{
|
by: maxw_cc |
last post by:
Hi to all of you,
I was wondering what the Semantics part
in C standard is really for? What should
be on the constraints part and what should
be on the semantics part?
Is the implementation obligued to produce
diagnostics only if I violate something in
the constraints part?
|
by: Paul Steckler |
last post by:
Here's some code that's giving me differing results, depending
on the compiler.
typedef foo {
int A,B;
} FOO;
int main() {
| |
by: Doug Wyatt |
last post by:
So I'll preface this with the fact that I'm a UNIX developer by training and
have just recently gotten in to C# development on Windows. I'm basically
running in to a problem whereby I suspect something to do with process
groups or threads and closeOnExec semantics (to speak in POSIX terms) is
causing me a problem. I've got a windows service (let's call it
"myService") that, among other things, does :
onStart
creates a TcpListener on a...
|
by: Dan Jacobson |
last post by:
How is this for correct HTML 4.01 headers?:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="zh-tw"><head>
<meta http-equiv="Content-Type" content=
"text/html; charset=utf-8">
<meta http-equiv="Content-Language" content="zh-tw">
And for my English pages, en-us instead of zh-tw.
Did I screw up any details? utf-8 or UTF-8 like Google?
The page should work via http:// or file:///.
|
by: dragoncoder |
last post by:
Just a simple theoritical question to the experts.
What was the rationale behind making STL containers follow copy
semantics rather than reference semantics. References almost always
make things easier without much of overhead. Then why not reference ?
Thanks
/P
|
by: =?Utf-8?B?QU1lcmNlcg==?= |
last post by:
I would like to define a structure or a class with an array field that
behaves like a simple value-semantics variable. For example, I want
something like
public structure polynomial
public a() as double ' the polynomial coefficients
...
end structure
that will behave like
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |