Python Tips: Difference between revisions
m (New page: Python takes some getting used to. The clean syntax is great, but it has some things which a Java or Ruby programmer might not guess. == How to tell if an object is None == This won't...) |
mNo edit summary |
||
Line 10: | Line 10: | ||
</geshi> | </geshi> | ||
This | This works: | ||
<geshi lang="python"> | <geshi lang="python"> | ||
if not bool(user): | |||
if not user: | |||
gologin() | gologin() | ||
</geshi> | </geshi> | ||
As does: | |||
<geshi lang="python"> | |||
if user == None: | |||
gologin() | |||
</geshi> | |||
But this is probably best: | |||
<geshi lang="python"> | <geshi lang="python"> | ||
if user is None: | if user is None: | ||
gologin() | gologin() | ||
</geshi> | </geshi> | ||
This article "[http://mail.python.org/pipermail/python-list/2003-March/192769.html what exactly is "None"]" helped explain this a little better. | |||
[[Category:Python]] |
Revision as of 11:02, 24 September 2008
Python takes some getting used to. The clean syntax is great, but it has some things which a Java or Ruby programmer might not guess.
How to tell if an object is None
This won't parse: <geshi lang="python"> user = users.get_current_user() if !user:
gologin()
</geshi>
This works: <geshi lang="python"> if not bool(user):
gologin()
</geshi>
As does: <geshi lang="python"> if user == None:
gologin()
</geshi>
But this is probably best: <geshi lang="python"> if user is None:
gologin()
</geshi>
This article "what exactly is "None"" helped explain this a little better.