Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


Search this Thread
Old 02-22-2013, 02:00 PM   #1
Registered: Jan 2013
Distribution: Debian
Posts: 157

Rep: Reputation: 4
python: mystery syntax

Hi. My Python skills are quite rusty, but I have to edit code somebody else wrote. I'm a little confused by this construction I found:

if not varname is None:
(I replaced the real variable with "varname" and the executed block with "...etc...")
What exact kind of check does "if not varname is None" do with varname?
Old 02-22-2013, 02:08 PM   #2
Senior Member
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 4,766

Rep: Reputation: 1467Reputation: 1467Reputation: 1467Reputation: 1467Reputation: 1467Reputation: 1467Reputation: 1467Reputation: 1467Reputation: 1467Reputation: 1467
It's the same as:

if varname is not None
Which can be run after lines like this:

varname = None
varname = 3
varname = 'a'
Old 02-22-2013, 02:56 PM   #3
Registered: Mar 2011
Location: Klaipėda, Lithuania
Distribution: Slackware
Posts: 248

Rep: Reputation: 107Reputation: 107
Operator is tests for object identity. From Python documentation:
Every object has an identity, a type and a value. An object’s identity never changes once it has been created; you may think of it as the object’s address in memory. The ‘is‘ operator compares the identity of two objects; the id() function returns an integer representing its identity (currently implemented as its address).
In your case the condition checks if object varname refers to is not a None object (a special singleton object, used where one needs to indicate the absence of a value).


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] python simple syntax ted_chou12 Programming 3 04-06-2012 10:01 PM
[SOLVED] Python Installed; bash command: python gets syntax error in cre84j Linux - Software 4 01-23-2011 04:46 PM
[SOLVED] How to do syntax highlighting in a python-qt application ? Snouffelaire Linux - Software 4 11-13-2009 03:19 AM
[python] syntax Error : invalid syntax Python_user Programming 2 09-06-2009 12:52 PM
Python MySQLdb syntax issues... elvenson Programming 4 12-18-2007 12:36 PM

All times are GMT -5. The time now is 11:52 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration