0

How can I pass in arguments (a, b, c) to a function for a quadratic formula without having to redefine them in the function? I know I could use self.a instead of just a inside the formula (same thing for b and c) but how do I pass in the argument self.a as a, self.b as b, and self.c into the function?

class Calc:

    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c
    
    def quadraticformula(self):
        c = self.c
        b = self.b 
        a = self.a
        
        neg = ((b*-1)-(sqrt((b**2)-4*a*c)))/(2*a)
        pos = ((b*-1)+(sqrt((b**2)-(4*a*c))))/(2*a)
        return (pos,neg)
tripleee
  • 175,061
  • 34
  • 275
  • 318
coffe1
  • 11
  • 1
    What's the point of that being a class at all? If you move the parameters from `__init__` to `quadraticformula`, then there's no instance state at all. – jonrsharpe Aug 30 '20 at 17:22
  • Rather use `def quadraticformula(self, a,b,c):` – Sociopath Aug 30 '20 at 17:23
  • @Sociopath But then the class would be pointless – wjandrea Aug 30 '20 at 17:23
  • You don't need to pass them into the function. You pass them when you create the `Calc` object. `c = Calc(1, 2, 3)` then `c.quadraticformula()` – Barmar Aug 30 '20 at 17:25
  • BTW this got me thinking about how to do plus/minus more easily, and I ended up posting [this answer](https://stackoverflow.com/a/63660407/4518341). In this case you could do `s = sqrt(b**2 - 4*a*c); pos, neg = ((-b+i) / (2*a) for i in (s, -s))`. – wjandrea Aug 30 '20 at 17:58
  • Also BTW, welcome to SO! Check out the [tour], and [ask] if you want advice. – wjandrea Aug 30 '20 at 18:00

2 Answers2

2

Instead of using a class with a constructor function just use a normal function in general

def calc(a, b, c):
    neg = ((b*-1)-(sqrt(b**2 - 4*a*c)))/(2*a)
    pos = ((b*-1)+(sqrt(b**2 - 4*a*c)))/(2*a)
    return pos, neg

Then call the function:

>>> calc(1, 2, -3)
(1.0, -3.0)
wjandrea
  • 28,235
  • 9
  • 60
  • 81
yungmaz13
  • 139
  • 11
  • 2
    like [Jack Diederich said](https://youtu.be/o9pEzgHorH0?t=180), *'The signature of "this shouldn't be a class" is that it has two methods, one of which is `__init__`.'* – wjandrea Aug 30 '20 at 17:55
0

You don't have to redefine anything. The __init__ method allows for all other methods of the class to be able to access that variable. So once you actually define a variable you passed to the class (you referenced it as a function, which its not) in the __init__ method all you have to do it just reference it with whatever operation you need.

# within you quadraticformula method
...
neg = ((self.b*-1)-(sqrt(self.b**2 - 4*self.a*self.c)))/(2*self.a)
pos = ((self.b*-1)+(sqrt(self.b**2 - 4*self.a*self.c)))/(2*self.a)
return pos, neg

When passing attributes to the class you have create an instance of it like so:

a = # something
b = # something
c = # something

cl = Calc(a, b, c)
cl.quadraticformula() # call the method (a function with a method) of the function here

# You can call this method in the __init__ method if you want to 
# execute as soon as you call the class instead of using the instance 
# to reference it
class Calc:
  def __init__(self,a,b,c):
     self.a = a
     self.b = b
     self.c = c
     self.quadraticformula
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Maxim
  • 276
  • 2
  • 6