introspection

Is there a built-in function to print all the current properties and values of an object?

好久不见. 提交于 2019-11-26 00:38:53
问题 So what I\'m looking for here is something like PHP\'s print_r function. This is so I can debug my scripts by seeing what\'s the state of the object in question. 回答1: You are really mixing together two different things. Use dir(), vars() or the inspect module to get what you are interested in (I use __builtins__ as an example; you can use any object instead). >>> l = dir(__builtins__) >>> d = __builtins__.__dict__ Print that dictionary however fancy you like: >>> print l ['ArithmeticError',

Getting method parameter names in Python

China☆狼群 提交于 2019-11-25 23:48:35
问题 Given the Python function: def a_method(arg1, arg2): pass How can I extract the number and names of the arguments. I.e., given that I have a reference to func , I want the func.[something] to return (\"arg1\", \"arg2\") . The usage scenario for this is that I have a decorator, and I wish to use the method arguments in the same order that they appear for the actual function as a key. I.e., how would the decorator look that printed \"a,b\" when I call a_method(\"a\", \"b\") ? 回答1: Take a look

How can you print a variable name in python? [duplicate]

泄露秘密 提交于 2019-11-25 23:41:36
问题 This question already has answers here : Can I print original variable's name in Python? (12 answers) Closed last year . Say I have a variable named choice it is equal to 2. How would I access the name of the variable? Something equivalent to In [53]: namestr(choice) Out[53]: \'choice\' for use in making a dictionary. There\'s a good way to do this and I\'m just missing it. EDIT: The reason to do this is thus. I am running some data analysis stuff where I call the program with multiple

Get an object properties list in Objective-C

故事扮演 提交于 2019-11-25 23:35:11
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, say, an NSArray of each of the (child) class properties as NSStrings that are not on the parent class,

Python: What does the slash mean in help() output?

旧街凉风 提交于 2019-11-25 23:31:26
问题 What does the / mean in Python 3.4\'s help output for range before the closing parenthesis? >>> help(range) Help on class range in module builtins: class range(object) | range(stop) -> range object | range(start, stop[, step]) -> range object | | Return a virtual sequence of numbers from start to stop by step. | | Methods defined here: | | __contains__(self, key, /) | Return key in self. | | __eq__(self, value, /) | Return self==value. ... 回答1: It signifies the end of the positional only

Getting the class name of an instance?

此生再无相见时 提交于 2019-11-25 23:07:43
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. sykora Have you tried the __name__ attribute of the class? ie type(x).__name__ will give you the name of the class, which I think is what you want. >>> import itertools >>> x = itertools.count(0) >>> type(x)._

How to get the caller's method name in the called method?

走远了吗. 提交于 2019-11-25 20:29:05
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? Alex Martelli inspect.getframeinfo and other related functions in inspect can help: >>> import inspect >>> def f1(): f2() ... >>> def f2(): ... curframe = inspect.currentframe() ... calframe = inspect.getouterframes(curframe, 2) ... print('caller name:', calframe[1][3]) ... >>> f1() caller name: f1 this