introspection

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

吃可爱长大的小学妹 提交于 2019-11-26 11:01: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? Rosh Oxymoron Python doesn't have a feature to access the function or its name within the function itself. It has been proposed but rejected. If you don't want to play with the stack yourself, you should either use "bar" or bar.__name__

Print all local variables accessible to the current scope in Lua

风格不统一 提交于 2019-11-26 09:27:20
问题 I know how to print \"all\" global variables using the following code for k,v in pairs(_G) do print(\"Global key\", k, \"value\", v) end So my question is how to do that for all variables that are accessible from the currently executing function, something that can do what locals() does for Python. 回答1: Here is an implementation of a locals() function. It will return a table of locals from the calling scope: function locals() local variables = {} local idx = 1 while true do local ln, lv =

List all base classes in a hierarchy of given class?

亡梦爱人 提交于 2019-11-26 09:06:22
问题 Given a class Foo (whether it is a new-style class or not), how do you generate all the base classes - anywhere in the inheritance hierarchy - it issubclass of? 回答1: inspect.getmro(cls) works for both new and old style classes and returns the same as NewClass.mro() : a list of the class and all its ancestor classes, in the order used for method resolution. >>> class A(object): >>> pass >>> >>> class B(A): >>> pass >>> >>> import inspect >>> inspect.getmro(B) (<class '__main__.B'>, <class '_

How to get the name of the current method from code [duplicate]

邮差的信 提交于 2019-11-26 08:50:36
问题 This question already has answers here : Can you use reflection to find the name of the currently executing method? (14 answers) Closed 5 years ago . I know you can do this.GetType().FullName To get My.Current.Class But what can I call to get My.Current.Class.CurrentMethod 回答1: using System.Diagnostics; ... var st = new StackTrace(); var sf = st.GetFrame(0); var currentMethodName = sf.GetMethod(); Or, if you'd like to have a helper method: [MethodImpl(MethodImplOptions.NoInlining)] public

what&#39;s the biggest difference between dir and __dict__ in python

人盡茶涼 提交于 2019-11-26 08:46:31
问题 class C(object): def f(self): print self.__dict__ print dir(self) c = C() c.f() output: {} [\'__class__\', \'__delattr__\',\'f\',....] why there is not a \'f\' in self.__dict__ 回答1: dir() does much more than look up __dict__ First of all, dir() is a API method that knows how to use attributes like __dict__ to look up attributes of an object. Not all objects have a __dict__ attribute though. For example, if you were to add a __slots__ attribute to your custom class, instances of that class won

Get property name as a string

随声附和 提交于 2019-11-26 08:10:53
问题 I need a way to pass a property and get the name assigned to it. Any suggestions? @property (nonatomic, retain) MyObject *crazyObject; NSString *str = SOME_WAY_TO_GET_PROPERTY_NAME(crazyObject); // Above method should return @\"crazyObject\" 回答1: You can try this: unsigned int propertyCount = 0; objc_property_t * properties = class_copyPropertyList([self class], &propertyCount); NSMutableArray * propertyNames = [NSMutableArray array]; for (unsigned int i = 0; i < propertyCount; ++i) { objc

How can I determine the type of a generic field in Java?

允我心安 提交于 2019-11-26 07:27:06
问题 I have been trying to determine the type of a field in a class. I\'ve seen all the introspection methods but haven\'t quite figured out how to do it. This is going to be used to generate xml/json from a java class. I\'ve looked at a number of the questions here but haven\'t found exactly what I need. Example: class Person { public final String name; public final List<Person> children; } When I marshall this object, I need to know that the chidren field is a list of objects of type Person , so

Using isKindOfClass with Swift

扶醉桌前 提交于 2019-11-26 06:17:22
问题 I\'m trying to pick up a bit of Swift lang and I\'m wondering how to convert the following Objective-C into Swift: - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesBegan:touches withEvent:event]; UITouch *touch = [touches anyObject]; if ([touch.view isKindOfClass: UIPickerView.class]) { //your touch was in a uipickerview ... do whatever you have to do } } More specifically I need to know how to use isKindOfClass in the new syntax. override func touchesBegan

Get __name__ of calling function&#39;s module in Python

守給你的承諾、 提交于 2019-11-26 06:15:01
问题 Suppose myapp/foo.py contains: def info(msg): caller_name = ???? print \'[%s] %s\' % (caller_name, msg) And myapp/bar.py contains: import foo foo.info(\'Hello\') # => [myapp.bar] Hello I want caller_name to be set to the __name__ attribute of the calling functions\' module (which is \'myapp.foo\') in this case. How can this be done? 回答1: Check out the inspect module: inspect.stack() will return the stack information. Inside a function, inspect.stack()[1] will return your caller's stack. From

Get all object attributes in Python?

时光总嘲笑我的痴心妄想 提交于 2019-11-26 06:12:24
问题 Is there a way to get all attributes/methods/fields/etc. of an object in Python? vars() is close to what I want, but it doesn\'t work unless an object has a __dict__ , which isn\'t always true (e.g. it\'s not true for a list , a dict , etc.). 回答1: Use the built-in function dir(). 回答2: What you probably want is dir(). The catch is that classes are able to override the special __dir__ method, which causes dir() to return whatever the class wants (though they are encouraged to return an accurate