1

I have a python script consisting of two classes in which i want to call function of one class inside function of another class. The structure look something as follows:

class A():
    def funct(arg1, arg2):
         B.other(arg)
         ....            
class B():
    def other(arg):
        ....

So , please help me how will i call function of class B inside class A funct?

anamika email
  • 327
  • 9
  • 21
  • you must to instanciate B and declare static the **other** method. PD: where is the **cls** argument? Also you can look an eye to this: http://www.jesshamrick.com/2011/05/18/an-introduction-to-classes-and-inheritance-in-python/ – mryuso92 Sep 10 '15 at 10:48
  • Highly related (and maybe duplicate of) [What is the difference between staticmethod and classmethod in Python?](http://stackoverflow.com/questions/136097/what-is-the-difference-between-staticmethod-and-classmethod-in-python). Also you can check [Python classmethod and staticmethod for beginner?](http://stackoverflow.com/questions/12179271/python-classmethod-and-staticmethod-for-beginner) – Mp0int Sep 10 '15 at 11:33

4 Answers4

0

try this

class A():
    def funct(self, arg1, arg2):
        B().other(arg1)

class B():
    def other(self, arg):
        print "inside other"

A().funct('arg1','arg2')
Gunjan
  • 2,775
  • 27
  • 30
0

you can do it in 2 ways

  1. Through object creation : if you want to use the methods of class A in class B, then create object A inside class B like obj = A() and call the methods using the reference variable obj like obj.function().
  2. Through inheritance: define class be like class B(A) and after that you can use all methods of class A inside class B
0

Define B's method as a @classmethod:

    class A(object):
        def funct(self, arg1, arg2):
            B.other(arg1)

    class B(object):
        @classmethod
        def other(cls, arg1):
            print "In B: " + arg1

    A().funct("arg", "arg2")
ChrisC73
  • 1,833
  • 14
  • 14
-1

Use classmethod:

class B(object):

    @classmethod
    def other(cls, arg1, arg2):
        print "I am function class B"

class A(object):
    def funct(self, arg1, arg2):
         B.other(arg1, arg2)

A().funct(1,2)

===>I am function class B

Use staticmethod:

class B(object):

    @staticmethod
    def other(arg1, arg2):
        print "I am function class B"

class A(object):
    def funct(self, arg1, arg2):
         B.other(arg1, arg2)

A().funct(1,2)

===>I am function class B

Some references about staticmethod and classmethod.

Instantiate B inside A class

class B(object):
    def other(self, arg1, arg2):
        print "I am function class B"

class A(object):
    def funct(self, arg1, arg2):
         B().other(arg1, arg2)

A().funct(1,2)
===>I am function class B

You could use Python Inheritance.

    class B(object):
        def other(self, arg=None):
            print "I am function class B"

    class A(B):
        def funct(self, arg1, arg2):
             self.other()

    a_class_object = A()
    a_class_object.other()

===>I am function class B

Also din't forgive to add self argument to class methods. You can find out more information about Inheritance in off. Python docs

Community
  • 1
  • 1
wolendranh
  • 4,202
  • 1
  • 28
  • 37