introspection

what's the biggest difference between dir and __dict__ in python

强颜欢笑 提交于 2019-11-26 23:33:09
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__ Martijn Pieters 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't have a __dict__ attribute, yet dir() can still list the available attributes on those instances:

Can a Python method check if it has been called from within itself?

走远了吗. 提交于 2019-11-26 23:10:28
问题 Let's say I have a Python function f and fhelp . fhelp is designed to call itself recursively. f should not be called recursively. Is there a way for f to determine if it has been called recursively? 回答1: Use the traceback module for this: >>> import traceback >>> def f(depth=0): ... print depth, traceback.print_stack() ... if depth < 2: ... f(depth + 1) ... >>> f() 0 File "<stdin>", line 1, in <module> File "<stdin>", line 2, in f None 1 File "<stdin>", line 1, in <module> File "<stdin>",

How Do I Perform Introspection on an Object in Python 2.x?

南楼画角 提交于 2019-11-26 23:06:31
问题 I'm using Python 2.x and I have an object I'm summoning from the aether; the documentation on it is not particularly clear. I would like to be able to get a list of properties for that object and the type of each property. Similarly, I'd like to get a list of methods for that object, as well, plus any other information I could find on that method, such as number of arguments and their respective types. I have a feeling that I am simply missing the correct jargon in my Google searches. Not

Inspect python class attributes

感情迁移 提交于 2019-11-26 22:21:17
问题 I need a way to inspect a class so I can safely identify which attributes are user-defined class attributes. The problem is that functions like dir(), inspect.getmembers() and friends return all class attributes including the pre-defined ones like: __class__ , __doc__ , __dict__ , __hash__ . This is of course understandable, and one could argue that I could just make a list of named members to ignore, but unfortunately these pre-defined attributes are bound to change with different versions

Java introspection: object to map

随声附和 提交于 2019-11-26 22:20:26
问题 I have a Java object obj that has attributes obj.attr1 , obj.attr2 etc. The attributes are possibly accessed through an extra level of indirection: obj.getAttr1() , obj.getAttr2() , if not public. The challenge : I want a function that takes an object, and returns a Map<String, Object> , where the keys are strings "attr1" , "attr2" etc. and values are the corresponding objects obj.attr1 , obj.attr2 . I imagine the function would be invoked with something like toMap(obj) , or toMap(obj, "attr1

Get property name as a string

﹥>﹥吖頭↗ 提交于 2019-11-26 22:08:43
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" 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_property_t property = properties[i]; const char * name = property_getName(property); [propertyNames addObject:

Python: How to retrieve class information from a 'frame' object?

折月煮酒 提交于 2019-11-26 19:56:30
问题 Is it possible to retrieve any class information from a frame object? I know how to get the file (frame.f_code.co_filename), function (frame.f_code.co_name) and line number (frame.f_lineno), but would like to be able to also get the name of the class of the active object instance of the frame (or None if not in an instance). 回答1: I don't believe that, at the frame object level, there's any way to find the actual python function object that has been called. However, if your code rely on the

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

£可爱£侵袭症+ 提交于 2019-11-26 19:47:22
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 I can marshall it properly. I had tried for (Field field : Person.class.getDeclaredFields()) { System.out

Watch for a variable change in python

风格不统一 提交于 2019-11-26 19:47:13
问题 There is large python project where one attribute of one class just have wrong value in some place. It should be sqlalchemy.orm.attributes.InstrumentedAttribute, but when I run tests it is constant value, let's say string. There is some way to run python program in debug mode, and run some check (if variable changed type) after each step throught line of code automatically? P.S. I know how to log changes of attribute of class instance with help of inspect and property decorator. Possibly here

How do I list available methods on a given object or package in Perl?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 19:23:21
问题 How do I list available methods on a given object or package in Perl? 回答1: If you have a package called Foo , this should do it: no strict 'refs'; for(keys %Foo::) { # All the symbols in Foo's symbol table print "$_\n" if exists &{"Foo::$_"}; # check if symbol is method } use strict 'refs'; Alternatively, to get a list of all methods in package Foo : no strict 'refs'; my @methods = grep { defined &{"Foo::$_"} } keys %Foo::; use strict 'refs'; 回答2: There are (rather too) many ways to do this