Variable creation and returning Boolean values Out of a challenge for myself, I created this little program in which you can create "variables", test those "variable" values, test regular values, or clear all the "variables".

from operator import *

oper = {
    "==": eq,
    "&gt;": gt,
    "&lt;": lt,
    "&lt;=": le,
    "&gt;=": ge,
    "!=": one,}

var = []

def println(stringint):
    print stringint

def clearvars():
    del var[:]

def createvar(value):
    var.append(value)

def returnbool(value1, testvalue, value2):
    print testvalue(value1, value2)


Here's an example of an interpreter session. If you're curious, pcl is the filename.

&gt;&gt;&gt; import pcl
&gt;&gt;&gt; pcl.createvar(1)
&gt;&gt;&gt; pcl.createvar(3)
&gt;&gt;&gt; pcl.println(pcl.var)
[1, 3]
&gt;&gt;&gt; pcl.returnbool(pcl.var[0], pcl.oper["!="], pcl.var[1])
True
&gt;&gt;&gt; pcl.clearvars()
&gt;&gt;&gt; pcl.println(pcl.var)
[]


All I'm really looking for are places where I could improve on, and how I can make this compatible with Python2 &amp; Python3.
The code is sufficiently trivial that it's hard to see where improvements could be made. The only element that isn't already compatible with Python 3 is println (print is a function in 3.x), which seems useless anyway - why would you do:

pcl.println(pcl.var)


rather than

print pcl.var


If you're determined to keep it,

def println(stringint):
    print(stringint)


will work in 2.x and 3.x.



One step up would be to go for a class:

from operator import ne, ... # avoid 'from xyz import *' 

class PCL(object):

    OPERATORS = {"!=": ne, ...}

    def __init__(self, vars=None): # provide initial variables (defaults to empty list)
        if vars is None:
            vars = []
        self.vars = vars

    def clear_vars(self): # create new list rather than deleting old one
        self.vars = []

    def create_var(self, value):
        self.vars.append(value)

    def return_bool(self, var1_index, op, var2_index):
        return self.OPERATORS[op](self.vars[var1_index], 
                                  self.vars[var2_index])


This allows you to have multiple "sessions" alongside each other, with separate variable lists. It also removes the reliance on scoping for variable access, which will make testing easier as this grows in complexity.

I have also altered return_bool to take the indices and operator as arguments directly, simplifying the syntax:

&gt;&gt;&gt; from pcl import PCL
&gt;&gt;&gt; pcl1 = PCL()
&gt;&gt;&gt; pcl1.create_var(1)
&gt;&gt;&gt; pcl1.create_var(3)
&gt;&gt;&gt; pcl1.return_bool(0, "!=", 1)
True


This may or may not be desirable.