Assign a value to a var content in an object
rosuav at gmail.com
Mon Oct 11 18:06:49 EDT 2021
On Tue, Oct 12, 2021 at 9:03 AM Paulo da Silva
<p_d_a_s_i_l_v_a_ns at nonetnoaddress.pt> wrote:
> Is there a better way of doing this?
> Why didn't setattr (as commented) work?
> Thanks for an help/comments.
> class C:
> def f(self,v):
Because setattr needs a string, just like the assignment to the
dictionary does. Try:
setattr(self, "n", v)
Or, since it's a constant:
self.n = v
More information about the Python-list