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

2
  • Please update your question with the full error traceback. Commented Aug 18, 2020 at 9:32
  • What is a and b in self.func(a,b)? and It's better to use Calcul as class name(first letter uppercase) Commented Aug 18, 2020 at 9:33

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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)

2 Comments

I get an error when running this: func() takes 2 positional arguments but 3 were given
my bad, i've edited it. func needs the self input parameter

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.