0

I want to use a function implemented inside a class as followed:

class calcul:
    def func(a,b):
        return log(a/b)
    def sec_func(c,d):
        out = func(c,d)
        return out

I've tried to initiate the function:

class calcul:
    def __init__(self):
        self.func(a,b)
    def func(a,b):
        return log(a/b)
    def sec_func(self,c,d):
        out = self.func(c,d)
        return out

however, I get 'func is not defined' as output

Keyvan Tajbakhsh
  • 103
  • 1
  • 10

2 Answers2

3

You are probably missing the self as first argument of your functions. Else you should declare them as @staticmethod. What you probably want to do is

class Calcul:
    def func(self,a,b):
        return log(a/b)
    def sec_func(self,c,d):
        out = self.func(c,d)
        return out
FreshD
  • 2,914
  • 2
  • 23
  • 34
1

You would create a class like so.

#define the class
class Calcul:
    def __init__(self):
        pass
        #pass becuase no attributes to store
    def func(self, a, b):
        return log(a/b)
    def sec_func(self,c,d):
        out = self.func(c,d)
        return out

#create a new instance of the class
calc_obj = Calcul()

#call the function using the class variable you have just defined.

ans = calc_obj.func(1,2)
ans1 = calc_obj.sec_func(1,2)
Lewis Morris
  • 1,916
  • 2
  • 28
  • 39