Re: [Pyobjc-dev] Access to 'hidden' class-methods
Brought to you by:
ronaldoussoren
From: Ronald O. <ous...@ci...> - 2003-01-12 07:17:06
|
On Sunday, Jan 12, 2003, at 05:17 Europe/Amsterdam, bb...@ma... wrote: > 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? No, that's why one of the tests in Lib/objc/tes/test_objc.py is failing. If a class has a class and instance method with the same name, the former will be inaccessible from Python using the normal mechanisme. In python, attributes are first looked up in the __dict__ of the instance and then in the __dict__ of its class (and superclasses). All methods are only present in the class dict. Note that my patch doesn't change the default behaviour, NSObject.alloc() will still work. > > 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. Sounds good, but methodForSelector: is simular to description in that it is both a class and instance methods, and in your example your using the class method. Because of the way the bridge and python are working you cannot access the class-method at the moment. Ronald |