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

S

Sam Sungshik Kong

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
 
I

Ivan Voras

Sam said:
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.
 
K

Kirk Strauser

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

Sam Sungshik Kong said:
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-----
 
J

John Roth

Sam Sungshik Kong said:
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
 
S

Sam Sungshik Kong

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
 
D

Donn Cave

Quoth "Sam Sungshik Kong" <[email protected]>:
....
| 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, (e-mail address removed)
 
M

Max M

Sam said:
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
 
O

Oliver Fromme

John Roth said:
> "Sam Sungshik Kong said:
> >
> > 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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top