obj.__dict__ or vars(obj)

Asked by Justin

Hi,

When I instantiate a class that uses Storm, vars(obj) and obj.__dict__ return unexpected results:
class Example(object):
 __storm_table__ = "example"
 id = Int(primary=True)
 attribute = Int()

 def __init__(self, attribute):
  self.attribute = attribute

>>> ex = Example(50)
>>> ex
<__main__.Example object at 0xb10ed0>
>>> dir(ex)
['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__storm_class_info__', '__storm_object_info__', '__storm_table__', '__str__', '__weakref__', '_storm_columns', 'attribute', 'id']
>>> vars(ex)
{'__storm_object_info__': {}}
>>> ex.__dict__
{'__storm_object_info__': {}}
>>> ex.id
>>> ex.attribute
50

In my opinion Storm breaks the semantics of vars() and __dict__.
Any idea how I could get a dict of all the attributes from an instance?

Cheers,

Justinus

Question information

Language:
English Edit question
Status:
Answered
For:
Storm Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Launchpad Janitor (janitor) said :
#1

This question was expired because it remained in the 'Open' state without activity for the last 15 days.

Revision history for this message
James Henstridge (jamesh) said :
#2

The column values are not stored in the instance dictionary, true.

The vars() method is not a reliable method of discovering the attributes on an object though. Consider the following non-storm example class:

    class C(object):
        __slots__ = ['__dict__', 'a']
        def __init__(self):
            self.a = 1
            self.b = 2
        @property
        def c(self):
            return 3

While instances of this class have attributes a, b and c, vars() will only tell you about 'b' because that's the only one that gets stored in the instance dictionary.

Can you help with this problem?

Provide an answer of your own, or ask Justin for more information if necessary.

To post a message you must log in.