By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,480 Members | 771 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,480 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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.
Jul 18 '05 #2

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

At 2004-05-12T17:12:00Z, "Sam Sungshik Kong" <ss*@chol.nospam.net> 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+Y0CpvERAvRPAJ0Uojjx9qMtOHKApnDMnt 3TxsL5ngCgnKWG
ejWI7Ycj5asnK9WsoWL11Eo=
=bL+r
-----END PGP SIGNATURE-----
Jul 18 '05 #3

P: n/a

"Sam Sungshik Kong" <ss*@chol.nospam.net> wrote in message
news:AV******************@newssvr29.news.prodigy.c om...
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

Jul 18 '05 #4

P: n/a
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" <ss*@chol.nospam.net> wrote in message
news:AV******************@newssvr29.news.prodigy.c om...
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

Jul 18 '05 #5

P: n/a
Sam Sungshik Kong wrote:
If it's not None, str(something) should not return "None".


What about str("None")?
Jul 18 '05 #6

P: n/a
Quoth "Sam Sungshik Kong" <ss*@chol.nospam.net>:
....
| 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, do**@drizzle.com
Jul 18 '05 #7

P: n/a
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
Jul 18 '05 #8

P: n/a
John Roth <ne********@jhrothjr.com> wrote:
"Sam Sungshik Kong" <ss*@chol.nospam.net> 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
Jul 18 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.