introspection

Can you list the keyword arguments a function receives?

时光毁灭记忆、已成空白 提交于 2019-11-26 06:05:27
问题 I have a dict, which I need to pass key/values as keyword arguments.. For example.. d_args = {\'kw1\': \'value1\', \'kw2\': \'value2\'} example(**d_args) This works fine, but if there are values in the d_args dict that are not accepted by the example function, it obviously dies.. Say, if the example function is defined as def example(kw2): This is a problem since I don\'t control either the generation of the d_args , or the example function.. They both come from external modules, and example

How to get current function into a variable?

大城市里の小女人 提交于 2019-11-26 05:26:37
问题 How can I get a variable that contains the currently executing function in Python? I don\'t want the function\'s name. I know I can use inspect.stack to get the current function name. I want the actual callable object. Can this be done without using inspect.stack to retrieve the function\'s name and then eval ing the name to get the callable object? Edit: I have a reason to do this, but it\'s not even a remotely good one. I\'m using plac to parse command-line arguments. You use it by doing

Ruby: kind_of? vs. instance_of? vs. is_a?

随声附和 提交于 2019-11-26 04:29:13
问题 What is the difference? When should I use which? Why are there so many of them? 回答1: kind_of? and is_a? are synonymous. instance_of? is different from the other two in that it only returns true if the object is an instance of that exact class, not a subclass. Example: "hello".is_a? Object and "hello".kind_of? Object return true because "hello" is a String and String is a subclass of Object . However "hello".instance_of? Object returns false . 回答2: What is the difference? From the

How can I get the name of an object in Python?

我是研究僧i 提交于 2019-11-26 03:41:13
问题 Is there any way to get the name of an object in Python? For instance: my_list = [x, y, z] # x, y, z have been previously defined for bla in my_list: print \"handling object \", name(bla) # <--- what would go instead of `name`? # do something to bla Edit: Some context: What I\'m actually doing is creating a list of functions that I can specify by the command line. I have: def fun1: pass def fun2 pass def fun3: pass fun_dict = {\'fun1\': fun1, \'fun2\': fun2, \'fun3\': fun3} I get the name of

Get an object properties list in Objective-C

╄→尐↘猪︶ㄣ 提交于 2019-11-26 03:24:28
问题 How can I get a list (in the form of an NSArray or NSDictionary ) of a given object properties in Objective-C? Imagine the following scenario: I have defined a parent class which just extends NSObject , that holds an NSString , a BOOL and an NSData object as properties. Then I have several classes which extend this parent class, adding a lot of different properties each. Is there any way I could implement an instance method on the parent class that goes through the whole object and returns,

Retrieving the inherited attribute names/values using Java Reflection

扶醉桌前 提交于 2019-11-26 03:08:23
问题 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? 回答1: no, you need to write it yourself

Objective-C Introspection/Reflection

一笑奈何 提交于 2019-11-26 01:56:22
问题 Is there a built in method, function, API, commonly accepted way, etc. to dump the contents of an instantiated object in Objective-C, specifically in Apple\'s Cocoa/Cocoa-Touch environment? I want to be able to do something like MyType *the_thing = [[MyType alloc] init]; NSString *the_dump = [the_thing dump]; //pseudo code NSLog(\"Dumped Contents: %@\", the_dump); and have the object\'s instance variable names and values displayed, along with any methods available to call at run time. Ideally

Determine function name from within that function (without using traceback)

情到浓时终转凉″ 提交于 2019-11-26 01:55:29
问题 In Python, without using the traceback module, is there a way to determine a function\'s name from within that function? Say I have a module foo with a function bar. When executing foo.bar() , is there a way for bar to know bar\'s name? Or better yet, foo.bar \'s name? #foo.py def bar(): print \"my name is\", __myname__ # <== how do I calculate this at runtime? 回答1: Python doesn't have a feature to access the function or its name within the function itself. It has been proposed but rejected.

How to get the caller&#39;s method name in the called method?

蓝咒 提交于 2019-11-26 01:44:29
问题 Python: How to get the caller\'s method name in the called method? Assume I have 2 methods: def method1(self): ... a = A.method2() def method2(self): ... If I don\'t want to do any change for method1, how to get the name of the caller (in this example, the name is method1) in method2? 回答1: inspect.getframeinfo and other related functions in inspect can help: >>> import inspect >>> def f1(): f2() ... >>> def f2(): ... curframe = inspect.currentframe() ... calframe = inspect.getouterframes

Getting the class name of an instance?

孤街醉人 提交于 2019-11-26 00:45:38
问题 How do I find out a name of class that created an instance of an object in Python if the function I am doing this from is the base class of which the class of the instance has been derived? Was thinking maybe the inspect module might have helped me out here, but it doesn\'t seem to give me what I want. And short of parsing the __class__ member, I\'m not sure how to get at this information. 回答1: Have you tried the __name__ attribute of the class? ie type(x).__name__ will give you the name of