Why not 'foo = not f' instead of 'foo = (not f or 1) and 0'?

Discussion in 'Python' started by Kristian Domke, Jan 23, 2008.

  1. Hello to all

    I am trying to learn python at the moment studying an example program
    (cftp.py from the twisted framework, if you want to know)

    There I found a line

    foo = (not f and 1) or 0

    In this case f may be None or a string.

    If I am not wrong here, one could simply write

    foo = not f

    because if f = None:

    (not f) = true,
    (true and 1) = true,
    (true or 0) = true

    or if f = 'bar'

    (not f) = false
    (false and 1) = false
    (false or 0) = false

    So why bothering with the longer version?

    I hope, I made clear, what I want...

    CU

    Kristian
     
    Kristian Domke, Jan 23, 2008
    #1
    1. Advertising

  2. Kristian Domke

    Guest

    On Jan 23, 9:45 am, Kristian Domke <> wrote:
    > Hello to all
    >
    > I am trying to learn python at the moment studying an example program
    > (cftp.py from the twisted framework, if you want to know)
    >
    > There I found a line
    >
    > foo = (not f and 1) or 0
    >
    > In this case f may be None or a string.
    >
    > If I am not wrong here, one could simply write
    >
    > foo = not f
    >
    > because if f = None:
    >
    > (not f) = true,
    > (true and 1) = true,
    > (true or 0) = true
    >
    > or if f = 'bar'
    >
    > (not f) = false
    > (false and 1) = false
    > (false or 0) = false
    >
    > So why bothering with the longer version?
    >
    > I hope, I made clear, what I want...
    >
    > CU
    >
    > Kristian


    f = None
    foo = (not f and 1) or 0
    # this gives you 1

    f = None
    foo = not f
    # this gives you True
     
    , Jan 23, 2008
    #2
    1. Advertising

  3. Kristian Domke

    Gary Herron Guest

    Kristian Domke wrote:
    > Hello to all
    >
    > I am trying to learn python at the moment studying an example program
    > (cftp.py from the twisted framework, if you want to know)
    >
    > There I found a line
    >
    > foo = (not f and 1) or 0
    >
    > In this case f may be None or a string.
    >
    > If I am not wrong here, one could simply write
    >
    > foo = not f
    >
    > because if f = None:
    >
    > (not f) = true,
    > (true and 1) = true,
    > (true or 0) = true
    >
    > or if f = 'bar'
    >
    > (not f) = false
    > (false and 1) = false
    > (false or 0) = false
    >
    > So why bothering with the longer version?
    >


    Good catch! It's my guess that you've found a way to improve on a bit
    of carelessly written code.

    However there *is* a (subtle) difference between
    not f
    and
    (not f and 1) or 0

    The first produces a boolean value, and the second produces an int
    value, but since one is a subclass of the other, you'd have to write
    quite perverse code care about the difference.

    Gary Herron

    > I hope, I made clear, what I want...
    >

    Quite.
    > CU
    >
    > Kristian
    >
     
    Gary Herron, Jan 23, 2008
    #3
  4. Kristian Domke

    Guest

    Sorry, posted to quickly.

    Yes your logic is correct about the "logic" of the return, but theirs
    actually differs in what it returns, and I am guessing it is an
    important change. Where is this "foo" used? Perhaps its value is used
    in a way a boolean return couldn't be?

    Just a note, with these kind of points it is often worth just opening
    up a python console and printing out the results. Though I do agree
    with you, at first look it seems almost redundant to use 1 and 0.
    Personally haven't seen this before, rather like it! :)
     
    , Jan 23, 2008
    #4
  5. Kristian Domke

    Jarek Zgoda Guest

    Gary Herron napisa³(a):

    > However there *is* a (subtle) difference between
    > not f
    > and
    > (not f and 1) or 0
    >
    > The first produces a boolean value, and the second produces an int
    > value, but since one is a subclass of the other, you'd have to write
    > quite perverse code care about the difference.


    Twisted sems to be perverted to the root.

    --
    Jarek Zgoda
    Skype: jzgoda | GTalk: | voice: +48228430101

    "We read Knuth so you don't have to." (Tim Peters)
     
    Jarek Zgoda, Jan 23, 2008
    #5
  6. Kristian Domke

    Duncan Booth Guest

    Kristian Domke <> wrote:

    > foo = (not f and 1) or 0
    >
    > In this case f may be None or a string.
    >
    > If I am not wrong here, one could simply write
    >
    > foo = not f
    >


    Yes, it sounds pretty silly, and not just on the level you spotted.

    The only difference between the two expressions is that the original sets
    foo to an integer whereas your version sets it to a bool. So the question
    of which is most appropriate actually comes down to what foo is being used
    for.

    Is there really some code which requires a numeric value of 1 when f is
    None or an empty string and a value of 0 for any other string? I can't
    think offhand of any obvious situations where you would want that. My guess
    is that foo is being used later as a condition in an 'if' statement.

    If you really do need an integer then in Python 2.5+ another way to write
    it would be:

    foo = 0 if f else 1

    Also 'foo' is a silly name since it gives no indication at about the
    purpose of the expression, but I'm hoping that was just you paraphrasing
    the code you posted.

    Ok, I just looked at the code, it is indeed being used as a boolean, so

    self.useProgressBar = not f
    or
    self.useProgressBar = f is not None

    if you want to be more specific about checking for None.
     
    Duncan Booth, Jan 23, 2008
    #6
  7. Kristian Domke

    Boris Borcic Guest

    I am surprised nobody pointed out explicitely that

    True==1 and False==0

    so that for instance

    5*(True+True)==10

    and even (but implementation-dependent) :

    5*(True+True) is 10

    BB
     
    Boris Borcic, Jan 23, 2008
    #7
  8. On Wed, 23 Jan 2008 09:30:28 +0000, Duncan Booth wrote:

    > Kristian Domke <> wrote:
    >
    >> foo = (not f and 1) or 0
    >>
    >> In this case f may be None or a string.
    >>
    >> If I am not wrong here, one could simply write
    >>
    >> foo = not f
    >>
    >>

    > Yes, it sounds pretty silly, and not just on the level you spotted.
    >
    > The only difference between the two expressions is that the original
    > sets foo to an integer whereas your version sets it to a bool. So the
    > question of which is most appropriate actually comes down to what foo is
    > being used for.



    But since Python bools are subclasses from int, both of them are actually
    ints. One happens to look like 1, and the other looks like True.



    --
    Steven
     
    Steven D'Aprano, Jan 23, 2008
    #8
  9. Kristian Domke

    Gary Herron Guest

    Boris Borcic wrote:
    > I am surprised nobody pointed out explicitely that
    >
    > True==1 and False==0
    >

    Several of us did indeed point this out by saying that bool's are a
    subclass of ints.
    > so that for instance
    >
    > 5*(True+True)==10
    >
    > and even (but implementation-dependent) :
    >
    > 5*(True+True) is 10
    >
    > BB
    >
    >
     
    Gary Herron, Jan 23, 2008
    #9
  10. Kristian Domke

    Duncan Booth Guest

    Steven D'Aprano <> wrote:

    > On Wed, 23 Jan 2008 09:30:28 +0000, Duncan Booth wrote:
    >
    >> Kristian Domke <> wrote:
    >>
    >>> foo = (not f and 1) or 0
    >>>
    >>> In this case f may be None or a string.
    >>>
    >>> If I am not wrong here, one could simply write
    >>>
    >>> foo = not f
    >>>
    >>>

    >> Yes, it sounds pretty silly, and not just on the level you spotted.
    >>
    >> The only difference between the two expressions is that the original
    >> sets foo to an integer whereas your version sets it to a bool. So the
    >> question of which is most appropriate actually comes down to what foo
    >> is being used for.

    >
    >
    > But since Python bools are subclasses from int, both of them are
    > actually ints. One happens to look like 1, and the other looks like
    > True.


    The twisted code isn't incorrect, just twisted. :)
     
    Duncan Booth, Jan 23, 2008
    #10
  11. Kristian Domke wrote:
    > I am trying to learn python at the moment studying an example program
    > (cftp.py from the twisted framework, if you want to know)
    >
    > There I found a line
    >
    > foo = (not f and 1) or 0


    Equivalent to ``foo = int(not f)``

    > In this case f may be None or a string.
    >
    > If I am not wrong here, one could simply write
    >
    > foo = not f


    No cast to int() here.

    STeVe
     
    Steven Bethard, Jan 23, 2008
    #11
  12. On Jan 23, 4:06 am, Gary Herron <> wrote:

    > However there *is* a (subtle) difference between
    > not f
    > and
    > (not f and 1) or 0
    >
    > The first produces a boolean value, and the second produces an int
    > value, but since one is a subclass of the other, you'd have to write
    > quite perverse code care about the difference.


    Even if for some reason I did want the result to be int, I would write
    it as "int(not f)".

    George
     
    George Sakkis, Jan 23, 2008
    #12
    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. Replies:
    3
    Views:
    399
    John Roth
    Jul 29, 2005
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,137
    Smokey Grindel
    Dec 2, 2006
  3. .rhavin grobert

    vector: Foo[5] == ((foo*)Foo) + 5 ?

    .rhavin grobert, Sep 23, 2008, in forum: C++
    Replies:
    4
    Views:
    414
    JaredGrubb
    Sep 24, 2008
  4. Replies:
    4
    Views:
    165
    Thomas 'PointedEars' Lahn
    Dec 23, 2007
  5. Peter
    Replies:
    2
    Views:
    296
    Öö Tiib
    Jun 6, 2013
Loading...

Share This Page