why do we use operator.abs

0 votes
In operators module we have a helper method operator.abs. But in python abs is already a function, and the only way I know to invoke the __abs__ method on an object is by function call anyway.

Is there some other fancy way I don't know about to take the absolute value of a number? If not, why does operator.abs exist in the first place, and what is an example where you would have to use it instead of plain old abs?
Apr 2 in Python by ana1504.k
• 7,350 points
16 views

2 answers to this question.

0 votes
abs(...)
    abs(a) -- Same as abs(a).

No, I don’t think there’s some other fancy way you don’t know about, or really any reason at all for this to be here at all except for consistency.
answered Apr 2 by SDeb
• 12,120 points
0 votes

 python and abs() in Python

The abs() take one argument and give absolute value. The argument can be an integer, a floating point number or a complex number.

  • If  argument is an integer or floating number, abs() returns the absolute value in integer or float.

  • if argument is  complex number abs() returns only the magnitude part and that can also be a floating point number

example

x = abs(-7.25)

output:7.25

answered Apr 3 by anonymous

Related Questions In Python

0 votes
2 answers

Why do we use return statement in python?

def maximum(x, y):     if x > y:     ...READ MORE

answered Apr 4 in Python by anonymous
30 views
0 votes
0 answers

how do i use the XOR operator in python regular expression?

can u give an example? READ MORE

Mar 26 in Python by Waseem
• 3,320 points
23 views
0 votes
1 answer

Why there is no do while loop in python

There is no do...while loop because there ...READ MORE

answered Aug 6, 2018 in Python by Priyaj
• 56,160 points
85 views
0 votes
1 answer

What is getattr() exactly and how do I use it?

You can view a full example here: http://www.diveintopython.net/power_of_introspection/index.html Introspection ...READ MORE

answered Sep 17, 2018 in Python by bug_seeker
• 15,310 points
11 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 121 views
0 votes
1 answer

Why do variables have bigger scopes in Python than in C?

Only functions, modules, and the bodies of ...READ MORE

answered Jul 2 in Python by SDeb
• 12,120 points
9 views
0 votes
1 answer

What are the ternary conditional operator in Python?

The Ternary Conditional operator was added in ...READ MORE

answered Sep 19, 2018 in Python by SDeb
• 12,120 points
42 views