[Tutor] Defining "bit" type -- why not '!' ?

Kent Johnson kent37 at tds.net
Thu Jan 29 16:22:02 CET 2009


On Thu, Jan 29, 2009 at 5:32 AM, spir <denis.spir at free.fr> wrote:

> I'm aware that "not 1" is not the same as "~1" in python. And I agree with that, while I disagree on how it is different. As I see it, both operators have a close meaning, so that using the same sign (and the same underlying method name, e.g. __not__) should be allright. This is a common practice in python and many languages that support OOP.
> The flaw here in python is to treat integers as logical values, like if they were bools. This is for me a conceptual error. So that:
>
> * If 'not' is only a plain logical operator, then "not 1" must raise TypeError.
> * On the hand hand, "extended logic", meaning apply logic operators to non-logic values (e.g. "if not s" instead of "if s==''" or "isEmpty(s)"), is seen by many as a useful feature.
> * If 'not' (or '!', or '~') has an extended meaning, and integers are additionally considered as byte sequences, then it is sensible to give "not 1" a semantics of bitwise not. So we don't need both "not 1" and "~1".

That way lies madness, you would have a situation where both these
expressions would be True for any non-zero integer:

  not(bool(x)) != bool(not(x))
  (x or not x) == False

Kent


More information about the Tutor mailing list