0

I have a method that returns a value object of a class like so:

def get_val_obj():
    return SomeValueObject()

I have another method that takes a dictionary and also keyword arguments:

def some_other_method(some_dict, some_other_argument, **kwargs):
    """Do some stuff"""

And finally within the calling method:

some_val_obj_1 = get_val_obj()
some_val_obj_2 = get_val_obj()

some_other_method(some_val_obj_1.__dict__, 'blah', **some_val_obj_2.__dict__)

I saw somewhere that if there is an init method within the Value Object that sets attributes, you can use

.__dict__

to convert a popo. Doesn't work, what do you think is the best approach to my problem? I could just straight up return a dictionary in get_val_obj(), but I'd like to keep it so that it returns a POPO. Is there some other approach entirely that I may be missing? Thanks for the help.

user1087973
  • 800
  • 3
  • 12
  • 29

1 Answers1

3

I believe what you are referring to is something along the lines of:

class Foo(object):

    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)


foo = Foo(a=1, b=2, c=3)
print foo.a
print foo.b
print foo.c

THis basically is just a class whose constructor updates it's internal attributes via it's __dict__ attribute. All? objects in Python have a __dict__ that holds the object's attributes.

See: Data Model

James Mills
  • 18,669
  • 3
  • 49
  • 62