Assign a value to a var content in an object

Paulo da Silva p_d_a_s_i_l_v_a_ns at nonetnoaddress.pt
Sun Oct 10 19:34:28 EDT 2021


Às 23:28 de 10/10/21, Stefan Ram escreveu:
> Paulo da Silva <p_d_a_s_i_l_v_a_ns at nonetnoaddress.pt> writes:
>> class C:
>>    def f(self,v):
>>        #setattr(self,n,v)
>>        self.__dict__['n']=v
> 
>> Why didn't setattr (as commented) work?
> 
>   Because the name n has not been initialized to a suitable
>   value in the function f. You could write
> 
> setattr( self, "n", v )
Ah, OK - I missed the "" around n! :-(
> 
>   , but if you want this,
> 
> self.n = v
> 
>   would be better.
Of course :-)
But that's not the purpose. This is just a toy example.

Thanks.


More information about the Python-list mailing list