Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- >>> class Foo:
- ... bar = 'hello'
- ... baz = 'world'
- ...
- >>> f = Foo()
- >>> props(f)
- { 'bar' : 'hello', 'baz' : 'world' }
- __dict__
- dir
- dir
- >>> 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' }
- 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
- dict((key, value) for key, value in f.__dict__.iteritems()
- if not callable(value) and not key.startswith('__'))
- class c(object):
- x = 3
- a = c()
Add Comment
Please, Sign In to add comment