Re: [Pyobjc-dev] Access to 'hidden' class-methods
Brought to you by:
ronaldoussoren
From: <bb...@ma...> - 2003-01-12 04:39:28
|
On Saturday, Jan 11, 2003, at 17:31 US/Eastern, Ronald Oussoren wrote: > Some time ago there was a discussion about access to class-methods > with the same name as an instance-method. Currently this is not > possible. > > One of suggestions was to add pyobjc-specific attributes to proxy > objects. I've implemented, but not yet fully tested this. After I > check this in you can do: > > NSObject.pyobjc_classMethods.description() # Call +description > a = NSObject.new() > a.pyobjc_classMethods.description() # Also call +description > a.pyobjc_instanceMethods.description() # Call -description > Could this be simplified by requiring the developer to go through class() to get to the class method? NSObject.pyobjc_classMethods.description() # invoke class method a = NSObject.new() a.class().description() # invoke class method a.description() # invoke instance method This doesn't solve the overall problem that Ronald's solution addresses, but it would work most of the time [I think]. > I've not yet added an pyobjc_instanceMethods attribute to > class-proxies. Another restriction is that reflection doesn't work yet > for the contents of these attributes, > dir(NSObject.pyobjc_classMethods) will not return a list of class > methods. Since we have to go through this extra hoop anyway, maybe the answer is to hang the classMethods and instanceMethods attribute off of the class object? a.class().pyobjc_classMethod('description') a.class().pyobjc_instanceMethod('description') Actually -- maybe the right thing to do is to bridge the following two methods on NSObject... - (IMP)methodForSelector:(SEL)aSelector; + (IMP)instanceMethodForSelector:(SEL)aSelector; ... such that they return unbound class/instance methods, as appropriate. This would make it possible to do: a.class().methodForSelector_("description")(a.class()) a.class().instanceMethodForSelector("description")(a) May look odd, but it is certainly 'pythonic' in that returning a callable from a method/function is fairly common practice. This also obviates the need for doing introspection at the level of generating an inventory of methods for the class with some categorization. > I'm not yet completely happy with this solution because you cannot > override these hidden methods in subclasses, that is you cannot define > a class-method 'description' in your subclass of NSObject(*). But > because this should rarely be necessary I can live with this. Still a problem with the above ... but more a problem with method declarations themselves. Maybe the answer is to have specific API for adding methods to a class. I.e.: a.class().pyobjc_addClassMethod("description", <callable>) a.class().pyobjc_addInstanceMethod("description", <callable>) The developer could declare this something like: class MyClass(NSObject): def class_description(self): ... do whatever here ... MyClass.pyobjc_addClassMethod("description", MyClass.class_description) Or something like that. b.bum |