vendredi 5 août 2016

Conditions in python [on hold]

  1. I'm surprised that all({}), all([]), all(()) are all True, while we all know bool({}), bool([]), bool(()) and any({}), any([]), any(()) are all 'False'. What is the reason for the true all({}), all([]), all(())?

  2. As for a = [1, 2, 3]; b = a[:], a is b is False, while for a = (1, 2, 3); b = a[:], a is b is True (they share the same address). I understand a is b if sets b = a. But why still so when set b = a[:], just because tuple is immutable?

  3. If s1, s2 = 'str', 'str', s1 is s2 is True, but s is ''.join(('s','t','r')) is False. This fact has been known well but I don't understand very well.

  4. I read some posts on SO and also some tutorials by google concerning __cmp__ and rich comparisons, but I still didn't get why somebodies prefer rich comparisons than __cmp__. It looks more convenient to implement the comparison in just one __cmp__ method enabling all of <, <=, ==, !=, >=, > work. Can anyone give some a concise and clear explanation?

Aucun commentaire:

Enregistrer un commentaire