Re: Re: Knowing about Inheritance

Paul Prescod <[email protected]> Thu, 29 Jul 2004 10:04:30 -0700
Newsgroups gmane.comp.lang.prothon.user
Message-ID <[email protected]>
Mark Hahn 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!).

> ... It just checks for classes containing attributes.  That would be
> trivial in Prothon using "attr in class.attrs_".

It isn't the checking that is of interest. It is the construction of a 
metaclass that does the checking automatically when the class (not the 
instance) is constructed. I thought that it would also check all 
subclasses of the class which would make it more interesting but it doesn't.

> Getting back to your original question:  Would it be alright do define
> inheritance as that which has gone through the call_ init_ process?  In that
> case it would be easy as you could just override the call_ or init_ for the
> class you want to track.

In this program, neither print statement ever happens:

object InterfaceChecker:
     "Validates the presence of required attributes"
     def call_():
         print("Called")
         obj = super.call_()
         return obj

     def init_():
         print("Inited")

object j(InterfaceChecker):
     pass

call_ and init_ only get executed when I actually call the prototype 
(i.e. "instantiate an instance" in Python terminology).

> You can't do it in general for any prototype link because anyone that owns a
> pointer to an object can use that pointer as a parent prototype link without
> the object's knowledge.  I could add a hook to the interpreter for this
> purpose if you wish.

It seems like it might be helpful for metaclassy applications. You might 
want to enforce policies about everything derived from your prototype. 
NOt a high priority but could be useful.

  Paul Prescod