[Tutor] no attribute

Lie Ryan lie.1296 at gmail.com
Fri Dec 12 01:08:43 CET 2008


On Thu, 11 Dec 2008 21:56:19 +0000, Alan Gauld wrote:

> "Bryan Fodness" <bryan.fodness at gmail.com> wrote
> 
>>I am trying to change values in a file.  The following code does not
>>seem to
>> find the attribute.
>>
>> def anonymize(obj, attr):
>>    try:
>>        obj.attr = 'Anonymize'
>>    except AttributeError:
>>        pass
> 
> This code is looking for an attribute of obj called attr It is not using
> the name of attr stored in the attr parameter.
> 
> To do that you will need to use the setattr function.
> 
> HTH

I don't think you'll even need setattr (for the piece of code given), 
because anonymize receives a fixed string. If that code is part of more 
complex code where the string might be anything setattr might be needed 
though.

What are you trying to do though?



More information about the Tutor mailing list