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,
    "<": lt,
    "<=": le,
    ">=": ge,
    "!=": one,}

var = []

def println(stringint):
    print stringint

def clearvars():
    del var[:]

def createvar(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.

>>> import pcl
>>> pcl.createvar(1)
>>> pcl.createvar(3)
>>> pcl.println(pcl.var)
[1, 3]
>>> pcl.returnbool(pcl.var[0], pcl.oper["!="], pcl.var[1])
>>> pcl.clearvars()
>>> 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 & 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:


rather than

print pcl.var

If you're determined to keep it,

def println(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):

    def return_bool(self, var1_index, op, var2_index):
        return self.OPERATORS[op](self.vars[var1_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:

>>> from pcl import PCL
>>> pcl1 = PCL()
>>> pcl1.create_var(1)
>>> pcl1.create_var(3)
>>> pcl1.return_bool(0, "!=", 1)

This may or may not be desirable.

  • \$\begingroup\$ Just curious, why should I avoid from whatever import *? \$\endgroup\$ Jul 4 '14 at 22:51
  • 1
    \$\begingroup\$ Per the style guide: "Wildcard imports (from <module> import *) should be avoided, as they make it unclear which names are present in the namespace, confusing both readers and many automated tools. ". See e.g. here for a real-world example. \$\endgroup\$
    – jonrsharpe
    Jul 4 '14 at 22:58
  • \$\begingroup\$ There is one flaw with this, I want returnbool() to be able to return the values of regular numbers. Not just the elements in list var. \$\endgroup\$ Jul 12 '14 at 17:48
  • \$\begingroup\$ @VladimirPutin well I'll leave that minor modification to you! \$\endgroup\$
    – jonrsharpe
    Jul 12 '14 at 18:13
  • \$\begingroup\$ I know, just wanted to let you know! \$\endgroup\$ Jul 12 '14 at 18:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.