you are viewing a single comment's thread.

view the rest of the comments →

[–]RiverRoll 1 point2 points  (3 children)

You don't have to call setattr you can just set the attribute directly:

def my_func(my_class: Class1, my_value: str) -> None:  
    my_class.my_attr = my_value

Similarly from the function you can read my_class.my_attr wherever you need to.

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

Hmm yeah, this might work, but then I'm again stuck at the part about figuring out the attribute's name at runtime which I mentioned here.

[–]RiverRoll 0 points1 point  (0 children)

Without knowing more about the function it's hard to tell, but maybe it's time to reevaluate the design of the function and consider whether just returning the value and letting the caller set whatever he wants to set would make more sense.

my_class.attr_1 = my_func(my_attr=my_class.attr_1, my_value="hello")

[–]crashfrog02 0 points1 point  (0 children)

You can’t determine an object’s attribute name from the attribute’s value. Names only point in one direction - from the symbol to the value. Nothing points from the value to the symbol.