introspection

Java introspection and reflection

点点圈 提交于 2019-11-26 15:22:19
问题 Can any one explain the use of Java reflection and introspection? When we need to use both? 回答1: Reflection (taken from oracle java tutorial) Reflection is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful

Checking for member existence in Python

对着背影说爱祢 提交于 2019-11-26 14:19:34
问题 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

Retrieving the inherited attribute names/values using Java Reflection

痴心易碎 提交于 2019-11-26 14:17:54
I've a Java object 'ChildObj' which is extended from 'ParentObj'. Now, if it is possible to retrieve all the attribute names and values of ChildObj, including the inherited attributes too, using Java reflection mechanism? Class.getFields gives me the array of public attributes, and Class.getDeclaredFields gives me the array of all fields, but none of them includes the inherited fields list. Is there any way to retrieve the inherited attributes also? dfa no, you need to write it yourself. It is a simple recursive method called on Class.getSuperClass() : public static List<Field> getAllFields

Introspection to get decorator names on a method?

牧云@^-^@ 提交于 2019-11-26 13:49:20
问题 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. 回答1: 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

How do I access properties of a javascript object if I don&#39;t know the names?

百般思念 提交于 2019-11-26 12:34:54
问题 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

Finding what methods a Python object has

邮差的信 提交于 2019-11-26 12:34:24
Given a Python object of any kind, is there an easy way to get the list of all methods that this object has? Or, if this is not possible, is there at least an easy way to check if it has a particular method other than simply checking if an error occurs when the method is called? ljs It appears you can use this code, replacing 'object' with the object you're interested in: object_methods = [method_name for method_name in dir(object) if callable(getattr(object, method_name))] I discovered it at this site . Hopefully, that should provide some further detail! For those of you getting

Why is important to include “.moc” file at end of a Qt Source code file?

旧街凉风 提交于 2019-11-26 12:32:38
问题 Why is it important to add an include for .moc file in a Qt cpp source code? This is a common step used in several Qt samples, including this one: http://doc.qt.io/qt-5/qttestlib-tutorial1-example.html; where the line #include \"testqstring.moc\" should be included in the end of the file. I don\'t understand exactly why this is necessary. Thanks. 回答1: It's necessary if you define QObject subclasses with the Q_OBJECT macro in a .cpp file. When you do so: qmake must generate rules inside your

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

99封情书 提交于 2019-11-26 12:14:38
问题 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

Checking Objective-C block type?

北城以北 提交于 2019-11-26 12:04:08
问题 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 =

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

冷暖自知 提交于 2019-11-26 11:24:18
问题 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