if Request("something") == None: doesn't work

Discussion in 'Python' started by Sam Sungshik Kong, May 12, 2004.

  1. Hello!

    I use Python for ASP programming.
    I found something weird.

    Response.Write(Request("something"))
    It draws "None" when there's no value for something.
    Actually I expect "" instead of "None".

    So I changed it like
    if Request("something") == None:
    Response.Write("")
    else:
    Response.Write(Request("something"))

    Strangely, the result of comparison is False.

    if str(Request("something")) == "None":
    works!

    Also,
    if len(Request("something")) == 0:
    works!

    What's wrong?

    ssk
    Sam Sungshik Kong, May 12, 2004
    #1
    1. Advertising

  2. Sam Sungshik Kong

    Ivan Voras Guest

    Sam Sungshik Kong wrote:

    > if str(Request("something")) == "None":
    > works!
    >
    > Also,
    > if len(Request("something")) == 0:
    > works!
    >


    Try Response.Write(repr(Request("something"))) to see if you are really
    getting a None.
    Ivan Voras, May 12, 2004
    #2
    1. Advertising

  3. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    At 2004-05-12T17:12:00Z, "Sam Sungshik Kong" <> writes:

    > if Request("something") == None:


    Have you tried:

    if Request("something") is None:

    > if str(Request("something")) == "None":


    Well, right. repr(None) == 'None'.

    > if len(Request("something")) == 0:
    > works!


    That seems kinda strange, granted.
    - --
    Kirk Strauser
    The Strauser Group
    Open. Solutions. Simple.
    http://www.strausergroup.com/
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.4 (GNU/Linux)

    iD8DBQFAonJC5sRg+Y0CpvERAvRPAJ0Uojjx9qMtOHKApnDMnt3TxsL5ngCgnKWG
    ejWI7Ycj5asnK9WsoWL11Eo=
    =bL+r
    -----END PGP SIGNATURE-----
    Kirk Strauser, May 12, 2004
    #3
  4. Sam Sungshik Kong

    John Roth Guest

    "Sam Sungshik Kong" <> wrote in message
    news:AVsoc.47661$...
    > Hello!
    >
    > I use Python for ASP programming.
    > I found something weird.
    >
    > Response.Write(Request("something"))
    > It draws "None" when there's no value for something.
    > Actually I expect "" instead of "None".
    >
    > So I changed it like
    > if Request("something") == None:
    > Response.Write("")
    > else:
    > Response.Write(Request("something"))
    >
    > Strangely, the result of comparison is False.
    >
    > if str(Request("something")) == "None":
    > works!
    >
    > Also,
    > if len(Request("something")) == 0:
    > works!
    >
    > What's wrong?


    Darned if I know. However, there are two comments.

    1. The standard way to check for None is to use the
    "is" operator, not the "==" operator. You might try
    that. The equals test ought to work, though.

    2. However, the even easier way to do it is not to do
    a check at all, but simply rely on the fact that both None
    and the null string act like False in an if statement. In
    other words, just remove the "== None" and see what
    happens.

    John Roth
    >
    > ssk
    >
    >
    John Roth, May 12, 2004
    #4
  5. Thanks for the replies.

    Based on the hints from the replies, I've tested some.

    When there's no argument in the request,

    Request("id") == None returns False
    if Request("id"): returns True
    str(Request("id")) returns "None"
    repr(Request("id")) returns <COMObject<unknown>>

    So my conclusion is that Request("id") is not None.
    It's an object, str() of which is accidentally "None".
    That confused me.

    If it's not None, str(something) should not return "None".

    ssk

    "Sam Sungshik Kong" <> wrote in message
    news:AVsoc.47661$...
    > Hello!
    >
    > I use Python for ASP programming.
    > I found something weird.
    >
    > Response.Write(Request("something"))
    > It draws "None" when there's no value for something.
    > Actually I expect "" instead of "None".
    >
    > So I changed it like
    > if Request("something") == None:
    > Response.Write("")
    > else:
    > Response.Write(Request("something"))
    >
    > Strangely, the result of comparison is False.
    >
    > if str(Request("something")) == "None":
    > works!
    >
    > Also,
    > if len(Request("something")) == 0:
    > works!
    >
    > What's wrong?
    >
    > ssk
    >
    >
    Sam Sungshik Kong, May 13, 2004
    #5
  6. Sam Sungshik Kong wrote:
    > If it's not None, str(something) should not return "None".


    What about str("None")?
    Leif K-Brooks, May 13, 2004
    #6
  7. Sam Sungshik Kong

    Donn Cave Guest

    Quoth "Sam Sungshik Kong" <>:
    ....
    | When there's no argument in the request,
    |
    | Request("id") == None returns False
    | if Request("id"): returns True
    | str(Request("id")) returns "None"
    | repr(Request("id")) returns <COMObject<unknown>>
    |
    | So my conclusion is that Request("id") is not None.
    | It's an object, str() of which is accidentally "None".
    | That confused me.

    It would confuse anyone. That's pretty bad.

    Donn Cave,
    Donn Cave, May 13, 2004
    #7
  8. Sam Sungshik Kong

    Max M Guest

    Sam Sungshik Kong wrote:

    > Hello!
    >
    > I use Python for ASP programming.
    > I found something weird.
    >
    > Response.Write(Request("something"))
    > It draws "None" when there's no value for something.
    > Actually I expect "" instead of "None".



    You could make it easier on yorself and you convert your Request object
    to a Python Dictionary instead.

    http://www.mxm.dk/products/public/iisUtils

    regards Max M
    Max M, May 13, 2004
    #8
  9. John Roth <> wrote:
    > "Sam Sungshik Kong" <> wrote:
    > >
    > > if Request("something") == None:
    > > Response.Write("")
    > > else:
    > > Response.Write(Request("something"))

    >
    > [...]
    > 2. However, the even easier way to do it is not to do
    > a check at all, but simply rely on the fact that both None
    > and the null string act like False in an if statement. In
    > other words, just remove the "== None" and see what
    > happens.


    Furthermore, in Python the result of a boolean operator is
    the value of the operand that has been evaluated last.
    So, the above if...else construct could be reduced to this
    simple (and very readable) line:

    Response.Write(Request("something") or "")

    Of course that assumes that the Request() really returns
    None, not a string with content "None".

    Best regards
    Oliver

    --
    "To this day, many C programmers believe that 'strong typing'
    just means pounding extra hard on the keyboard."
    -- Peter van der Linden
    Oliver Fromme, May 13, 2004
    #9
    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. David Freeman
    Replies:
    8
    Views:
    7,616
    tcena9
    Feb 16, 2011
  2. length power
    Replies:
    2
    Views:
    67
    Rustom Mody
    Apr 10, 2014
  3. Skip Montanaro
    Replies:
    0
    Views:
    50
    Skip Montanaro
    Apr 10, 2014
  4. Johannes Schneider

    Re: why i have the output of [None, None, None]

    Johannes Schneider, Apr 10, 2014, in forum: Python
    Replies:
    0
    Views:
    44
    Johannes Schneider
    Apr 10, 2014
  5. Terry Reedy
    Replies:
    0
    Views:
    53
    Terry Reedy
    Apr 10, 2014
Loading...

Share This Page