Playing with Prothon today, I am fascinated by the idea of eliminatingclasses in Python. I''m trying to figure out what fundamental benefitthere 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 = 0def __init__(self):Multi.numInstances += 1def printNumInstances():print "Number of Instances:", Multi.numInstancesprintNumInstances = staticmethod(printNumInstances)def cmeth(cls, x):print cls, xcmeth = 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 = 0def .__init__(): # instance methodMulti.numInstances += 1def .printNumInstances(): # static methodprint "Number of Instances:", Multi.numInstancesdef .cmeth(x): # class methodprint 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 asyntactic shortcut (substiting ''.'' for ''self'') By eliminating thisexplicit passing of the self object, Prothon makes all method formsthe same and eliminates a lot of complexity. It''s beginning to looklike the complexity of Python classes is unecessary. My question for the Python experts is -- What user benefit are wemissing if we eliminate classes? -- Dave 解决方案 It''s certainly true that in a prototype based language all objectsexist: there are no objects that the compiler deals with but doesnot put into the resulting program. And it''s quite true that it doesopen up the floodgates for a lot of messiness. On the other hand, there are application areas where that isexactly what you need: I''ve already mentioned interactive fiction,and there are undoubtedly others. I''d like to play around with a prototype based language to see how itworks, although for reasons I''ve mentioned elsewhere, I won''t useProthon. I''d be using IO if they had a windows executable installer,rather than requiring me to compile the silly thing. John Roth 这篇关于Prothon原型与Python类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-25 03:42