RE: Re: Knowing about Inheritance
"Mark Hahn" <[email protected]> Thu, 29 Jul 2004 10:38:25 -0700
| Newsgroups | gmane.comp.lang.prothon.user |
|---|---|
| Message-ID | <000201c47592$da2e7810$0b01a8c0@mark> |
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 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.
I understand now.
>> 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).
Yes I understand.
>> 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.
So do you think an object-based callback hook that notifies you whenever an
object is being used as a prototype would enable metaclasses in Prothon? Is
this the missing piece? If so I can add that easily without any giant
performance hit.
def check(obj):
if not $attr in obj.attrs_:
print "egads! obj doesn't have $attr!"
InterfaceChecker.addProtoCallback(check)
The timing would have to be careful. It would have to be called back after
the object block finished so all the code in the object block could finish
defining all the methods. Would we make this just a feature of the object
statement or have it work on any method of assigning a prototype?
We need to get this straightened out by Sunday in case anyone asks about
metaclasses (metatypes?) in Prothon.