Re:class proposal 2
"Lucius, Michael" <[email protected]>
| Newsgroups | gmane.comp.lang.prothon.user |
|---|---|
| Message-ID | <[email protected]> |
> ... It's prototype list will point to other classes > giving the normal class inheritance heirarchy. I assume you mean the class object will have the same protoypes as the instance object. This means the class object will be an extension of the same prototypes as the instance object and so the class instance will not have access to the instance's methods/data. Or is 'self' going to be set to be the instance? > ... Access to class methods inside a > normal method or inside a class definition would be through the normal > "Klass.class.method()" or through new syntax sugar that makes use of the > class keyword as a prefix "class.method()". How about the other way around? Is there a special way for class methods to access 'normal' methods that are defined in the instance or is this not allowed? Or must class methods access other class methods in a special way? > --- When an attribute is first being looked up in an immediate object, not > an ancestor, and a attribute named "class" exists in the immediate object, > that class attribute object is used in the object's place for the > lookup. --- > So under certain circumstances a special 'class scope' is inserted at the head (or near the head) of the scope chain? > class Klass(Parent1, Parent2): > """Class document string""" > > class: Can 'class:' be used in an 'object' block or only in a 'class' block? Here is my proposal: When defining an Object Klass: add a function class_() that defines an inner class. This inner class has Klass as its prototype and defines call_(), str_() and whatever else the class needs and returns that object. This inserts the 'class scope' as your proposal does by using existing functionality and does not require anything special be done in the language. Then you have: KlassFactory = Klass.class_() # returns Klass object that acts like a class newKlass = KlassFactory() # uses call_() from 'class scope' KlassFactory.str_() # uses str_() fron 'class scope' Note that KlassFactory has access to all Klass methods/data just like any instance has access to its prototype(s) methods and data. However the object returned by class_() can: Override any Klass methods (e.g. call_() str_()) Add any new data/methods it needs Intercept method calls to do something special Klass() will do want you want if call_() for the instance is defined as: def call_(args): return (self.class_())(args) # not sure of the syntax! Of course this behaviour can be modified by the programmer. Klass.str_() # invokes instance str_() Klass.class_().str_() # invokes class str_() KlassFactory.str_() # also invokes class str_() When defining a class Klass: add a function object_() which defines the behavior as an object instance. So object_() is like the above class_() but behaves like a regular instance. This means that the default behaviour is class behaviour and Klass.object_() returns an object that has put instance methods/data at the head of the scope chain. This introduces a difference between object and class. object: acts like an instance by default, but can be used as a class if desired. class: acts like a class by default, but can easily used as an instance. I think this leverages the ability of protoypes to delegate automatically