Prothon Prototypes vs Python Classes
David MacQuigg
dmq at gain.com
Sat Mar 27 20:28:34 EST 2004
Playing with Prothon today, I am fascinated by the idea of eliminating
classes in Python. I'm trying to figure out what fundamental benefit
there is to having classes. Is all this complexity unecessary?
Here is an example of a Python class with all three types of methods
(instance, static, and class methods).
# Example from Ch.23, p.381-2 of Learning Python, 2nd ed.
class Multi:
numInstances = 0
def __init__(self):
Multi.numInstances += 1
def printNumInstances():
print "Number of Instances:", Multi.numInstances
printNumInstances = staticmethod(printNumInstances)
def cmeth(cls, x):
print cls, x
cmeth = classmethod(cmeth)
a = Multi(); b = Multi(); c = Multi()
Multi.printNumInstances()
a.printNumInstances()
Multi.cmeth(5)
b.cmeth(6)
Here is the translation to Prothon.
Multi = Object()
with Multi:
.numInstances = 0
def .__init__(): # instance method
Multi.numInstances += 1
def .printNumInstances(): # static method
print "Number of Instances:", Multi.numInstances
def .cmeth(x): # class method
print Multi, x
a = Multi(); b = Multi(); c = Multi()
Multi.printNumInstances()
a.printNumInstances()
Multi.cmeth(5)
b.cmeth(6)
Note the elimination of 'self' in these methods. This is not just a
syntactic shortcut (substiting '.' for 'self') By eliminating this
explicit passing of the self object, Prothon makes all method forms
the same and eliminates a lot of complexity. It's beginning to look
like the complexity of Python classes is unecessary.
My question for the Python experts is -- What user benefit are we
missing if we eliminate classes?
-- Dave
More information about the Python-list
mailing list