Menghan Zheng wrote:
Hello!

Is it assured the following statement is always True?
If it is always True, in which version, python2.x or python3.x?

a = dict()
...
assert(a.values == [a[k] for k in a.keys()])
--> ?


Menghan Zheng

No, it's never true. The assert statement has no return value, neither True nor False.

But probably you're asking whether the assert statement will succeed quietly. Again, the answer is no. The first part of the expression is a built-in method, and the second part is a (possibly-empty) list. So it'll always throw an AssertionError.

But probably you've got a typo, and meant to include the parentheses:
  assert(a.values() == [a[k] for k in a.keys()])

That, I believe, is guaranteed to not fire the assertion in 2.6.

In 2.6, the docs say:

"If items(), keys(), values(), iteritems(), iterkeys(), and itervalues() are called with no intervening modifications to the dictionary, the lists will directly correspond"

In 3.x it should fire an assertion error, for any dictionary, because values() 
does not return a list, but an iterator for one.  However, I don't have the 
docs for 3.x handy, I just tried it interactively to confirm my belief.

DaveA


--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to