[Tutor] __abs__() not acting as expected

Dave Angel davea at davea.name
Mon Mar 24 06:43:53 CET 2014


 Jim Byrnes <jf_byrnes at comcast.net> Wrote in message:
> I am reading Practical Programming - An Introduction to Computer Science 
> Using Python 3.  They give this example:
> 
>  >>> abs(-3)
> 3
> 
>  >>> -3 .__abs__()
> 3
> 

Ben is right,  dunder methods don't belong in introductory texts.
 And they seldom should be called directly;  they're there for
 folks who are defining new classes that want to mimic behavior of
 builtins. 

But he didn't show you the simplest fix:

(-3).__abs__()

-- 
DaveA



More information about the Tutor mailing list