0

I am trying to figure out what kind is an object in python (is a method? is a class? is a static method?) using attributes, for example:

Iterating over objects present in __dict__ attribute object of my module: If an object has an attribute called __class__ and the __name__ atribute of it equals to "class" then it is a class. If not, it is a variable defined in my module (of class whatever __name__ has returned).

If an object has an attribute called "func_name" is a method. The issue arises when I encounter so called properties. A property object prop.attr("__class__").attr("__name__")" will return "property". But how could I extract the type of the property? I want to know if it is a float, an integer, etc.

>>>Vector3 is a class
>>>Vector3 has a member Y (property)
>>><property object at 0x000002840809E278>
>>>Y property attributes: ['__class__', '__delattr__', '__delete__', '__doc__', '__format__', '__get__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__set__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'deleter', 'fdel', 'fget', 'fset', 'getter', 'setter']

I am not able to use the getter, __get__, etc because I don't have an instance of an object to call this property over it (getter takes an object as argument).

For context, I am inspecting these objects from C++ code using Boost.Python, but as I am using just attributes I think it is not relevant for the solution. Just to justify why I am not using a more regular method, say with inspect module.

Sturm
  • 3,968
  • 10
  • 48
  • 78
  • Maybe the answers to this question can help you out a bit? https://stackoverflow.com/questions/1549801/what-are-the-differences-between-type-and-isinstance – ChatterOne Sep 06 '18 at 09:45
  • I have tried `isinstance(prop, float)` but it returns `False` for float properties – Sturm Sep 06 '18 at 09:53
  • 1
    Works fine with me. If I put a property in a class and assign something like `0.0` to it, then `isinstance(prop, float)` returns `True`. Maybe you should post the code that you tried? – ChatterOne Sep 06 '18 at 09:56
  • `vecClass = MyModule["Vector3"]` `propClass = vecClass["Y"]` `print isinstance(propClass, float)` `>>> False` – Sturm Sep 06 '18 at 10:09
  • @ChatterOne I suspect that you are performing this check with a particular instance, not the *class* object – Sturm Sep 06 '18 at 10:11
  • `class T:``brb = 0.0``print(isinstance(T.brb, float))` This prints `True`. If this is not what you meant, please post example code that can be copied/pasted and tried. – ChatterOne Sep 06 '18 at 10:19
  • I think you are getting true because of the initialization of the variable, also, it is not a property. Furthermore, I wanted to use attributes instead of calling methods, since I am operating from C++ – Sturm Sep 06 '18 at 10:35

1 Answers1

0

I think this is not possible due to the dynamic nature of Python.

I was able to solve it by documenting the properties and reading __doc__ attribute

Sturm
  • 3,968
  • 10
  • 48
  • 78
  • 1
    I finally understood what you meant initially and you're correct: you can't. As a suggestion for next time, please include a complete code example, as that would have made things a lot clearer from the beginning. – ChatterOne Sep 06 '18 at 11:55