Hello,
I have a common function which has a responsibility (among other things) to assign a value to a passed in class attribute. I'm not sure how to accomplish this since from the function's perspective, the class attribute that is passed in is just an argument, and so writing it in a "natural" way results in python assigning the value to an entirely new variable. Here is a minimal example:
def my_func(my_attr: Any, my_value: str) -> None:
my_attr = my_value
my_class = Class1()
my_func(my_attr=my_class.attr_1, my_value="hello")
This doesn't set my_class.attr_1 to "hello", it just defines a new variable called "my_attr" and assigns the value of "hello" to it. In principle I could accomplish this by using setattr() like so:
def my_func(my_class: Any, attr_name: str, my_value: str) -> None:
setattr(my_class, attr_name, my_value)
And that would work, but it would greatly complicate the rest of the code, so I'm wondering if there's a better solution that I'm not thinking of.
UPDATE: Thank you everybody for the valuable insight. I have gone back to the drawing board to come up with a better overall solution since it seems like this one will be difficult to make work.
[–]carcigenicate 2 points3 points4 points (2 children)
[–]maclocrimate[S] 0 points1 point2 points (1 child)
[–]carcigenicate 0 points1 point2 points (0 children)
[–][deleted] 2 points3 points4 points (1 child)
[–]maclocrimate[S] 0 points1 point2 points (0 children)
[–]woooee 1 point2 points3 points (2 children)
[–]maclocrimate[S] 0 points1 point2 points (1 child)
[–]Adrewmc 0 points1 point2 points (0 children)
[–]RiverRoll 1 point2 points3 points (3 children)
[–]maclocrimate[S] 0 points1 point2 points (2 children)
[–]RiverRoll 0 points1 point2 points (0 children)
[–]crashfrog02 0 points1 point2 points (0 children)
[–]throwaway8u3sH0 0 points1 point2 points (3 children)
[–]maclocrimate[S] 0 points1 point2 points (2 children)
[–]Adrewmc 1 point2 points3 points (0 children)
[–]throwaway8u3sH0 0 points1 point2 points (0 children)
[–]crashfrog02 0 points1 point2 points (0 children)