LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 02-22-2013, 02:00 PM   #1
stateless
Member
 
Registered: Jan 2013
Distribution: Debian
Posts: 166
Blog Entries: 1

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:

Code:
if not varname is None:
    ...etc...
(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
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,221

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

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

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

Rep: Reputation: 199Reputation: 199
Operator is tests for object identity. From Python documentation:
Quote:
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).
 
  


Reply



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 base.py. gets syntax error in base.py 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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:21 PM.

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