UsayHi
UsayHi

Reputation: 45

Simplify dict creation from non-defined type object

I'm trying to keep a bit of code as efficient as possible and I think the aspect of it is just messy. If anyone want's to give a suggestion for a simplification of it I would really apreciate it.

result = DotDict()
for name in dir(obj):
    value = getattr(obj, name)
    if not name.startswith('__') and not ismethod(value):
        result[name] = value
return result

In this case, the DotDict is a user class, derived from the dict class, being the only important thing the notion that it follows the same process that a dict does. The obj variable will any time, but will be mainly used as a class object from other classes.

I've seen a loop inside the dict creation but since I need both the name and value for the creation I couldn't make it work.

Upvotes: 0

Views: 24

Answers (0)

Related Questions