[Tutor] What protocol to follow when need to pick either one from __getattr__ and __getattribute__ ?

Alan Gauld alan.gauld at yahoo.co.uk
Tue Apr 23 18:58:35 EDT 2019


On 23/04/2019 15:52, Arup Rakshit wrote:

> If I replace __getattr__ with __getattribute__ I found the program works 
> exactly same. Now my questions is in real world when  you have to pick 
> between these 2 pair of special method which protocols a Python dev 
> checks to pick either of the one? Is there any such thing, or either one 
> is fine. Can anyone elaborate this to educate me please?

There are some recomendations but I'll leave it to someone
better qualified than me to respond.

> Another question:
> 
> My question is that: Can I raise a domain error like 
> OperationNotPermitted when raising instead of AttributeError ?

You can raise any kind of error you like however, in general, using the
standard error types is better because if your class gets using in
another program that already handles the standard error type then your
class will get handled too. But, if you raise a non standard error, it
may slip through the net. (You can mitigate this by subclassing from the
standard error that would normally be raised since catch will catch all
subclasses as swell as the stated exception type.)

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list