<p>Do you know if there is a built-in function to build a dictionary from an arbitrary object? I'd like to do something like this:</p>

<pre><code>&gt;&gt;&gt; class Foo:
...     bar = 'hello'
...     baz = 'world'
...
&gt;&gt;&gt; f = Foo()
&gt;&gt;&gt; props(f)
{ 'bar' : 'hello', 'baz' : 'world' }
</code></pre>

<p><strong>NOTE:</strong> It should not include methods. Only fields.</p>

The
```
dir
```
builtin will give you all the object's attributes, including special methods like
```
__str__
```
,
```
__dict__
```
and a whole bunch of others which you probably don't want. But you can do something like:
```
```
>>> class Foo(object):
...     bar = 'hello'
...     baz = 'world'
...
>>> f = Foo()
>>> [name for name in dir(f) if not name.startswith('__')]
[ 'bar', 'baz' ]
>>> dict((name, getattr(f, name)) for name in dir(f) if not name.startswith('__')) 
{ 'bar': 'hello', 'baz': 'world' }
```
```
So can extend this to only return data attributes and not methods, by defining your
```
props
```
function like this:
```
```
import inspect

def props(obj):
    pr = {}
    for name in dir(obj):
        value = getattr(obj, name)
        if not name.startswith('__') and not inspect.ismethod(value):
            pr[name] = value
    return pr
```
```