Python Tips: Difference between revisions

From EggeWiki
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 parses but doesn't work:
This works:
<geshi lang="python">
<geshi lang="python">
user = users.get_current_user()
if not bool(user):
if not user:
     gologin()
     gologin()
</geshi>
</geshi>


This works!
As does:
<geshi lang="python">
if user == None:
    gologin()
</geshi>
 
But this is probably best:
<geshi lang="python">
<geshi lang="python">
user = users.get_current_user()
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 07: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.