Can a simple a==b 'hang' in and endless loop?
steve at holdenweb.com
Wed Jan 18 16:32:10 CET 2006
Claudio Grondi wrote:
> Steve Holden wrote:
> The problem here is, that I mean, that in Python it makes no sense to
> talk about a value of an object, because it leads to weird things when
> trying to give a definition what a value of an object is.
I don;t understand why you say that.
> It seems, that in Python there is a lack of operator able to compare
> there are no really such things as values, so there is no need to
> compare them.
That is simply incorrect. The expression a == b is true under
well-defined circumstances usually referred to as "a and b having the
same value". There *are* such things as values. Presumably since you
know about "is", you also know about "id()". In C Python the id() of an
object is simply its memory address, though that isn't how id() is
defined. The fact remains that each object's id() must be unique at any
point in time (though id()s can be reused).
>>> a = 10987
>>> b = 10987
>>> a == b
See. Two different objects with the same value, so they compare equal.
Perhaps the difference you are having problems understanding arises
because Python also defines equality for complex objects.
>>> lst1 = [a, b]
>>> lst2 = [b, a]
>>> lst1 == lst2
Unlike C there is no need to compare each element of the lists:
list.__eq__() automatically iterates over the items of the list, and
returns True only if all pairwise comparisons return True.
> The higher level of abstraction/indirection in Python results in making
> the concepts of 'value', 'having a value' or 'comparing values' useless,
> where it helps in C to express the difference between address and
> content at that address and to distinguish between the information
> telling _what_ is stored in memory and the information about _where_ it
> is stored.
Well in Python each name in a namespace, and each attribute of an
object, and each item in a sequence or a mapping, is a reference to a
value. Where the value is stored is completely irrelevant, and addresses
are only available as an implementation detail in CPython. You can test
if two references are to the same object using "is", which you already
know. In other words
a is b is exactly id(a) == id(b)
Obviously "a is b" implies "a == b", whereas the converse is not true.
> It appears to me, that the whole subject of what an identifier in Python
> represents and if there is such thingy as 'value' in Python, is not
> perfectly well understood even by Python experts programming also in
> many other languages and therefore the vital differences in concepts
> behind Python and e.g. C/C++ is only poorly documented . What else would
> be the reason for the over-long discussion in the "Is 'everything' a
> refrence or isn't it?" thread in this newsgroup?
It may so appear to you, but that doesn't make it the case. The reason
for the overly-long discussion is that people insist on picking the
smallest details to bits and arguing over small semantic differences
that are irrelevant to getting programs working.
I have an idea that the people who wrote and maintain the interpreter
find these concepts quite comprehensible, and I don't have any problem
myself. I suspect you are externalising your confusion.
> From what I know about Python up to now, the problem with the concept
> of 'value' in Python is, that it depends ... and it depends on so many
> things, that it is hard to give any general valid statement about it
> without writing an entire chapter full of details and special cases.
Well, you really are making something extraordinarily easy much more
difficult than it needs to be. I don't even really understand why you
need a written definition of "what a value is", come to that.
Why don't you write some C that you thing it would be difficult to
rewrite it Python?
There really is no need to be so obsessive about where things are
stored. It simply doesn't matter in a language with no pointer types.
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/
More information about the Python-list