## Demo of simple Object Oriented programming in Python ## defining a class and its methods ## initializing an instance ## creating a new instance def vacuum_oo (path): f = open(path) for line in f.readlines(): percept = eval(line) action = Rule.getMatch(percept) print "Percept: ", percept , "and Action: " , action raw_input("Press any key to exit") f.close() A, B, dirty, clean, suck, right, left = 'A','B','dirty','clean', \ 'suck', 'right','left' class Rule: rules = [] # a class-level variable: list of the rules def __init__(self, leftHandSide, rightHandSide): self.lhs = leftHandSide self.rhs = rightHandSide Rule.rules.append(self) def getMatch(percept): for r in Rule.rules: if percept == r.lhs: return r.rhs return False getMatch = staticmethod(getMatch) Rule ([A, dirty], suck) Rule ([B, dirty], suck) Rule ([A, clean], right) Rule ([B, clean], left)