[Tutor] Why do I not get an error when I mistakenly type "humdrum.sigh_strenght" instead of the correct "humdrum.sigh_strength"?

Steven D'Aprano steve at pearwood.info
Mon Jan 25 05:24:01 EST 2016


On Sat, Jan 23, 2016 at 04:25:01PM -0600, boB Stepp wrote:

> I think I now have this nuked out.  I am only just now realizing how
> powerful .__dict__ is:
[...]
>             self.__dict__[attribute_name] = attribute_value

Indeed, but generally speaking you hardly ever need to manually operate 
with a __dunder__ method or attribute. They're not quite private, but 
they are reserved, and normally you would use the public interface.

Instead of obj.__dict__[name], one should use one of the attribute 
functions:

getattr(obj, name)
setattr(obj, name, 999)
delattr(obj, name)

Instead of obj.__dict__, one should use vars(obj).

And of course, we never write obj.__len__() when we can write len(obj) 
instead. And so forth.



-- 
Steve


More information about the Tutor mailing list