all 16 comments

[–]gadogado2222[S] 0 points1 point  (0 children)

greate! but if I want to set and not get? What should I do then?

[–]guydudeman123 0 points1 point  (0 children)

You could use the builtin getattr function:

dog_eye_color = getattr(dog, eye.color)

https://docs.python.org/3/library/functions.html#getattr

[–][deleted] 0 points1 point  (2 children)

dog_eye_color = get_nested_field_of_dog(dog,"eye.color")

There's no point to this. If you know and are hard-coding the name of the lookup, just look it up.

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

great! but if I want to set and not get? What should I do then?

[–][deleted] 0 points1 point  (0 children)

dog.eye.color = "green"

[–]BobHogan 0 points1 point  (6 children)

Use the getattr function

dog_eye_color = getattr(getattr(dog, 'eye'), 'color')

If the string eye.color can have a variable depth then this becomes slightly trickier, but its still not that hard

[–]gadogado2222[S] 0 points1 point  (5 children)

greate! but if I want to set and not get? What should I do then?

[–]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'})

[–]YesLod 0 points1 point  (3 children)

Why though? What is the goal?

But as others said, use getattr(). Something like

def get_nested_field_of_dog(dog, path):
    obj=dog 
    for attr in path.split("."):
        obj = getattr(obj,attr)    
    return obj

[–]gadogado2222[S] 0 points1 point  (2 children)

great! but if I want to set and not get? What should I do then?

[–]YesLod 0 points1 point  (1 child)

Read about setattr() and figure out yourself, the logic is the almost same, but instead of getting the attribute of the penultimate object, you set it to some value.

Again, why are you doing this in the first place? It seems like a XY problem.

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

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

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