-2

I have two python files as

one.py

class FirstClass:
    @classmethod
    def myClass(cls,first, second):
        return first+second

two.py

from one import FirstClass

class SecondClass:
    @classmethod
    def herClass(cls, val1, val2):
        FirstClass.myClass(val1,val2)

ob = SecondClass()
print(ob.herClass(2,3))

How can I access the classmethod of one class from the classmethod of another class. If not possible, what might be the possible solution. FirstClass needs to remain same, I have flexibility changing the method type on SecondClass.

sunil shrestha
  • 27
  • 3
  • 11

2 Answers2

2

It's possible, but you're missing the return of this function

def herClass(cls, val1, val2):
        return FirstClass.myClass(val1,val2)
Chien Nguyen
  • 648
  • 5
  • 7
0

There is nothing wrong with your implementation just add return statement in SecondClass.herClass

Corrected code will look like this

class SecondClass:
    @classmethod
    def herClass(cls, val1, val2):
        return FirstClass.myClass(val1,val2)
Arpit Svt
  • 1,153
  • 12
  • 20