Seems like I want a pre-processor, but...

Russell Warren russandheather at gmail.com
Tue Mar 28 11:14:47 EST 2006


After some digging it seems that python does not have any equivalent to
C's #if directives, and I don't get it...

For example, I've got a bit of python 2.3 code that uses
collections.deque.pop(0) in order to pop the leftmost item.  In python
2.4 this is no longer valid - there is no argument on pop (rightmost
only now) and you call .popleft() instead.

I would like my code to work in both versions for now and simply want
to add code like:

if sys.version[:3] == "2.3":
  return self.myDeque.pop(0)
else:
  return self.myDeque.popleft()

but am recoiling a bit at the unnecessary conditional in there that I
think will be run on every execution - unless the compiler has some
magic detection of things like sys.version to compile out the
conditional as if it were a preprocessor directive (seems highly
unlikely!)?.

What is the pythonic thing to do?  This is in a generally usable file,
not a package, so I don't want to make a version for each (which seems
to be what every package out there does).  It seems to be begging for a
pre-processor directive set.

Thanks,
Russ




More information about the Python-list mailing list