How to modify meaning of builtin function "not" to "!"?

Discussion in 'Python' started by grbgooglefan, May 9, 2008.

  1. grbgooglefan

    grbgooglefan Guest

    I am creating functions, the return result of which I am using to make
    decisions in combined expressions.
    In some expressions, I would like to inverse the return result of
    function.

    E.g. function contains(source,search) will return true if "search"
    string is found in source string.
    I want to make reverse of this by putting it as:
    if ( ! contains(s1,s2) ):
    return 1

    I found that "!" is not accepted by Python & compile fails with
    "invalid syntax".
    Corresponding to this Boolean Operator we've "not" in Python.

    How can I make "not" as "!"?
    grbgooglefan, May 9, 2008
    #1
    1. Advertising

  2. grbgooglefan

    Ivan Voras Guest

    grbgooglefan wrote:
    > I am creating functions, the return result of which I am using to make
    > decisions in combined expressions.
    > In some expressions, I would like to inverse the return result of
    > function.
    >
    > E.g. function contains(source,search) will return true if "search"
    > string is found in source string.
    > I want to make reverse of this by putting it as:
    > if ( ! contains(s1,s2) ):
    > return 1
    >
    > I found that "!" is not accepted by Python & compile fails with
    > "invalid syntax".
    > Corresponding to this Boolean Operator we've "not" in Python.
    >
    > How can I make "not" as "!"?


    "not" is a perfectly valid boolean operator in Python and means just
    what "!" means in C. The equivalent binary operator is "~", just like in C.

    >>> print not True

    False
    >>> print not 1

    False
    >>> print not 0

    True
    >>> print ~1

    -2
    >>> print ~0

    -1



    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.6 (GNU/Linux)

    iD8DBQFIJFeIldnAQVacBcgRAgOqAJ44VoYv275VxN0j32X3TBo/jx/D2ACgyp2w
    grnFynwn5ULYufMTmpOj5WU=
    =EKDP
    -----END PGP SIGNATURE-----
    Ivan Voras, May 9, 2008
    #2
    1. Advertising

  3. grbgooglefan

    Guest

    On May 9, 8:41 am, grbgooglefan <> wrote:
    > I am creating functions, the return result of which I am using to make
    > decisions in combined expressions.
    > In some expressions, I would like to inverse the return result of
    > function.
    >
    > E.g. function contains(source,search) will return true if "search"
    > string is found in source string.
    > I want to make reverse of this by putting it as:
    > if ( ! contains(s1,s2) ):
    >      return 1
    >
    > I found that "!" is not accepted by Python & compile fails with
    > "invalid syntax".
    > Corresponding to this Boolean Operator we've "not" in Python.
    >
    > How can I make "not" as "!"?


    I have found that not 8, not not 8, and ~8 are all valid. Valid8 is
    not.

    NameError: name 'Valid8' is not defined, where defined is not defined,
    and @Valid8 @Valid8 ...

    Late-binding logicals are fine. #Valid8
    , May 9, 2008
    #3
  4. grbgooglefan a écrit :
    > I am creating functions, the return result of which I am using to make
    > decisions in combined expressions.
    > In some expressions, I would like to inverse the return result of
    > function.
    >
    > E.g. function contains(source,search) will return true if "search"
    > string is found in source string.


    Do you really need a function for this ?

    if "foo" in "foobar":
    print "do you really really need a function for this ?"


    > I want to make reverse of this by putting it as:
    > if ( ! contains(s1,s2) ):
    > return 1


    which is a convoluted way to write:

    return s2 not in s1


    > I found that "!" is not accepted by Python & compile fails with
    > "invalid syntax".
    > Corresponding to this Boolean Operator we've "not" in Python.
    >
    > How can I make "not" as "!"?


    Err... How to say... The boolean negation operator in Python is "not".
    So the only answer I can provide is : "use 'not' instead of '!'". And
    while we're at it : drop those useless parens.

    HTH
    Bruno Desthuilliers, May 9, 2008
    #4
  5. > -----Original Message-----
    > From: python-list-bounces+jr9445= [mailto:python-
    > list-bounces+jr9445=] On Behalf Of grbgooglefan
    > Sent: Friday, May 09, 2008 9:41 AM
    > To:
    > Subject: How to modify meaning of builtin function "not" to "!"?
    >
    > I am creating functions, the return result of which I am using to make
    > decisions in combined expressions.
    > In some expressions, I would like to inverse the return result of
    > function.
    >
    > E.g. function contains(source,search) will return true if "search"
    > string is found in source string.
    > I want to make reverse of this by putting it as:
    > if ( ! contains(s1,s2) ):
    > return 1
    >
    > I found that "!" is not accepted by Python & compile fails with
    > "invalid syntax".
    > Corresponding to this Boolean Operator we've "not" in Python.
    >
    > How can I make "not" as "!"?



    Serious question: Why would you want to? 'not' is easier to read (and type) than '!'. Mentally, when you see '!' you think 'not'. It's also harder to overlook 'not', especially when compared to '!contains()'. Finally, I imagine that Spanish speaking coders suffer enormous mental anguish when they see a right-side up '!' at the beginning of a sentence.

    if ( ! contains(s1,s2) ):
    return 1

    if ( !contains(s1,s2) ):
    return 1

    if ( not contains(s1,s2) ):
    return 1

    if ( ¡contains(s1,s2)):
    return 1




    *****

    The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. GA625
    Reedick, Andrew, May 9, 2008
    #5
  6. grbgooglefan

    Terry Reedy Guest

    "grbgooglefan" <> wrote in message
    news:...
    |I am creating functions, the return result of which I am using to make
    | decisions in combined expressions.
    | In some expressions, I would like to inverse the return result of
    | function.
    |
    | E.g. function contains(source,search) will return true if "search"
    | string is found in source string.
    | I want to make reverse of this by putting it as:
    | if ( ! contains(s1,s2) ):
    | return 1
    |
    | I found that "!" is not accepted by Python & compile fails with
    | "invalid syntax".
    | Corresponding to this Boolean Operator we've "not" in Python.
    |
    | How can I make "not" as "!"?

    1. Download, edit, and compile Python source code.
    Perhaps substituting '!' for 'not' in the grammar file will suffice.
    I do not *think* that this would conflict with '!=' digraph.
    2. Realize that code for your custom Python will not work anywhere else if
    it does contain '!' with that meaning.
    Terry Reedy, May 9, 2008
    #6
  7. grbgooglefan

    Lie Guest

    On May 9, 8:41 pm, grbgooglefan <> wrote:
    > I am creating functions, the return result of which I am using to make
    > decisions in combined expressions.
    > In some expressions, I would like to inverse the return result of
    > function.
    >
    > E.g. function contains(source,search) will return true if "search"
    > string is found in source string.
    > I want to make reverse of this by putting it as:
    > if ( ! contains(s1,s2) ):
    >      return 1
    >
    > I found that "!" is not accepted by Python & compile fails with
    > "invalid syntax".
    > Corresponding to this Boolean Operator we've "not" in Python.
    >
    > How can I make "not" as "!"?


    Are you trying to make Python a C? Realize that when you work with a
    language, you play by that language's rule, not by your rule. This
    seems like a troll or something.
    Lie, May 18, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Caspy
    Replies:
    2
    Views:
    8,311
    Caspy
    Oct 14, 2005
  2. User
    Replies:
    0
    Views:
    305
  3. Kiran
    Replies:
    6
    Views:
    400
    Christopher Benson-Manica
    Jan 18, 2007
  4. Replies:
    4
    Views:
    736
  5. bdb112
    Replies:
    2
    Views:
    289
    Chris Torek
    Jul 2, 2011
Loading...

Share This Page