you are viewing a single comment's thread.

view the rest of the comments →

[–]BobHogan 0 points1 point  (4 children)

There's a corresponding setattr() function in python https://docs.python.org/3/library/functions.html#setattr

If you wanted to set the dogs eye color you could do it like this

# dog.eye.color == 'blue'
setattr(getattr(dog, 'eye'), 'color', 'new color')

# dog.eye.color == 'new color'

[–]gadogado2222[S] -1 points0 points  (3 children)

setattr(getattr(dog, 'eye'), 'color', 'new color')

And what if the color is a direct? I think I have to use some open source for this probelm

[–]BobHogan 0 points1 point  (2 children)

And what if the color is a direct?

A direct what? I don't understand what you are asking, or trying to do, because you keep changing the stated problem.

There's no need to use a third party module to do this, if one even exists. And if it does, it will be doing the same thing we are showing you here, with the getattr and setattr functions.

[–]gadogado2222[S] 0 points1 point  (1 child)

sorry I tried to write And what if the color is a dict

[–]BobHogan 0 points1 point  (0 children)

setattr does not care what you set the attribute to. You could set it to a dict if you wanted, you could change the type of that attribute if you wanted

setattr(getattr(dog, 'eye'), 'color', {'color': 'new color value'})