Re[2]: Re: Knowing about Inheritance

Lenard Lindstrom <[email protected]> Fri, 30 Jul 2004 20:59:33 -0700 (Pacific Daylight Time)
Newsgroups gmane.comp.lang.prothon.user
Message-ID <Mahogany-0.66.0-4294658727-20040730-212033.00@pop3.norton.antivirus>
On Thu, 29 Jul 2004 10:38:25 -0700 Mark Hahn <[email protected]> wrote:

>  Paul Prescod wrote:
> 
> >>>I have an object that would like to know whenever it is inherited 
> >>>from. In Python you do this with metaclasses. How do you do it in 
> >>>Prothon?
> >>>
> >>>I'm trying to implement this recipe:
> >>>
> >>>http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/204349
> >> 
> >> 
> >> Are you sure you gave me the correct link?  That is "Metaclass for 
> >> Interface Checking" and it has no capability of detecting what has 
> >> inherited from what.
> >
> >I thought that if you subclassed MyClass it would trigger the 
> >behaviour 
> >of InterfaceChecker for every subclass you create. But I guess I 
> >misunderstand Python metaclasses (who doesn't!).
> 
> Oh, you may be right.  I thought your end goal was to find out the instances
> of a class.  I understand now you are just concerned whether a metclass type
> operation can be done in Python or not.
> 
> I am concerned also and there is some question about that.  Lenard made a
> claim at one point that Prothon couldn't do a metaclass and when I called
> him on it he backed off and said he didn't know for sure.  I added an item
> to the Wiki for the 1.0 release to find out and make sure it can but it
> ended up on the future-release list.
>
It has to do with the way Python's class statement works. Unlike the object
statement the body of the class statement is evaluated before the actual
class object is created. The local scope of the class statement is passed
to the class constructor function - metaclass - as a dictionary. So a user
provided class creation function can scan the dictionary, which contains
class attributes, and decide what to do with them. It also gets the
inheritance list so it can manipulate that as well. So a user provided
metaclass can add additional attributes and parent classes behind the scene.

In Python metaclasses are inherited. That is how new-style classes work.
They all inherit object's metaclass. So entirely new class systems can
be created simply by inheriting from a common root class. I do not know
if simular lineages are possible with prototypes. It may involve overriding
call_.

All I can say is that metaclassing requires a language where an object's
type, including a class object, is defined by a class. In a prototype based
language all objects have the same implicit type and it is hardcoded into
the interpreter. Whether or not this limits prototype versatility I do not
know. It may be prototypes provide equally useful alternatives to metaclasses.
I have not had enough experience with either prototypes or useful metaclass
examples to make a judgment.

Lenard Lindstrom
<[email protected]>