introspection

How do I find all the property keys of a KVC compliant Objective-C object?

孤街浪徒 提交于 2019-11-27 10:01:04
问题 Is there a method that returns all the keys for an object conforming to the NSKeyValueCoding protocol? Something along the lines of [object getPropertyKeys] that would return an NSArray of NSString objects. It would work for any KVC-compliant object. Does such a method exist? I haven't found anything in searching the Apple docs so far. Thanks, G. 回答1: #import "objc/runtime.h" unsigned int outCount, i; objc_property_t *properties = class_copyPropertyList([self class], &outCount); for(i = 0; i

Checking for member existence in Python

为君一笑 提交于 2019-11-27 08:50:36
I regularly want to check if an object has a member or not. An example is the creation of a singleton in a function. For that purpose, you can use hasattr like this: class Foo(object): @classmethod def singleton(self): if not hasattr(self, 'instance'): self.instance = Foo() return self.instance But you can also do this: class Foo(object): @classmethod def singleton(self): try: return self.instance except AttributeError: self.instance = Foo() return self.instance Is one method better of the other? Edit: Added the @classmethod ... But note that the question is not about how to make a singleton

Introspection to get decorator names on a method?

回眸只為那壹抹淺笑 提交于 2019-11-27 08:14:45
I am trying to figure out how to get the names of all decorators on a method. I can already get the method name and docstring, but cannot figure out how to get a list of decorators. If you can change the way you call the decorators from class Foo(object): @many @decorators @here def bar(self): pass to class Foo(object): @register(many,decos,here) def bar(self): pass then you could register the decorators this way: def register(*decorators): def register_wrapper(func): for deco in decorators[::-1]: func=deco(func) func._decorators=decorators return func return register_wrapper For example: def

Introspecting a given function's nested (local) functions in Python

杀马特。学长 韩版系。学妹 提交于 2019-11-27 07:07:22
问题 Given the function def f(): x, y = 1, 2 def get(): print 'get' def post(): print 'post' is there a way for me to access its local get() and post() functions in a way that I can call them? I'm looking for a function that will work like so with the function f() defined above: >>> get, post = get_local_functions(f) >>> get() 'get' I can access the code objects for those local functions like so import inspect for c in f.func_code.co_consts: if inspect.iscode(c): print c.co_name, c which results

Fastest/One-liner way to list attr_accessors in Ruby?

China☆狼群 提交于 2019-11-27 06:54:04
What's the shortest, one-liner way to list all methods defined with attr_accessor ? I would like to make it so, if I have a class MyBaseClass , anything that extends that, I can get the attr_accessor 's defined in the subclasses. Something like this: class MyBaseClass < Hash def attributes # ?? end end class SubClass < MyBaseClass attr_accessor :id, :title, :body end puts SubClass.new.attributes.inspect #=> [id, title, body] What about to display just the attr_reader and attr_writer definitions? There is no way (one-liner or otherwise) to list all methods defined by attr_accessor and only

Retrieve module object from stack frame

柔情痞子 提交于 2019-11-27 06:46:50
问题 Given a frame object, I need to get the corresponding module object. In other words, implement callers_module so this works: import sys from some_other_module import callers_module assert sys.modules[__name__] is callers_module() (That would be equivalent because I can generate a stack trace in the function for this test case. The imports are there simply to make that example complete and testable, and prevent callers_module from taking the shortcut of using __name__, since it's in a

Checking Objective-C block type?

こ雲淡風輕ζ 提交于 2019-11-27 06:34:20
This is primarily a curiosity, I'm not really sure what's the practical use of this but here goes. Since blocks are also Objective-C objects, is it possible to check their type? That is, does it respond to the isKindOfClass: message and how to use that message with respect to blocks? My naive thought that it's probably like this: -(void) aMethod { typedef int (^BlockA)(int x, int y); id blockVar = ...; // get a block from somewhere if([blockVar isKindOfClass:BlockA]) { BlockA blockVarA = blockVar; int result = blockVarA(1,2); } } The code above probably won't work. But if it is possible to

How do you get all classes defined in a module but not imported?

喜你入骨 提交于 2019-11-27 04:57:09
I've already seen the following question but it doesn't quite get me where I want: How can I get a list of all classes within current module in Python? In particular, I do not want classes that are imported, e.g. if I had the following module: from my.namespace import MyBaseClass from somewhere.else import SomeOtherClass class NewClass(MyBaseClass): pass class AnotherClass(MyBaseClass): pass class YetAnotherClass(MyBaseClass): pass If I use clsmembers = inspect.getmembers(sys.modules[__name__], inspect.isclass) like the accepted answer in the linked question suggests, it would return

Introspect calling object

落花浮王杯 提交于 2019-11-27 03:13:42
问题 How do I introspect A 's instance from within b.func() (i.e. A 's instance's self ): class A(): def go(self): b=B() b.func() class B(): def func(self): # Introspect to find the calling A instance here 回答1: In general we don't want that func to have access back to the calling instance of A because this breaks encapsulation. Inside of b.func you should have access to any args and kwargs passed, the state/attributes of the instance b (via self here), and any globals hanging around. If you want

How do I access properties of a javascript object if I don't know the names?

倾然丶 夕夏残阳落幕 提交于 2019-11-27 02:41:34
Say you have a javascript object like this: var data = { foo: 'bar', baz: 'quux' }; You can access the properties by the property name: var foo = data.foo; var baz = data["baz"]; But is it possible to get these values if you don't know the name of the properties? Does the unordered nature of these properties make it impossible to tell them apart? In my case I'm thinking specifically of a situation where a function needs to accept a series of name-value pairs, but the names of the properties may change. My thoughts on how to do this so far is to pass the names of the properties to the function