-1

I'm fairly new to object oriented programming and have a question on the following.

class TestClass():
    def test(arg1,arg2):
        global message
        print(arg1,arg2)
        message=str(arg1)+' '+str(arg2)
        print message

    def test0( self, twoword ):       
        global word1,word2
        word1=twoword.split('-')[0]
        word2=twoword.split('-')[1]
        print(word1,word2)
        TestClass.test(word1,word2)

The functionality of the program is redundant and can be simplified but I wish to call the test() from test(). How would one go about this? Thank you.

zweed4u
  • 207
  • 5
  • 18
  • This doesn't answer your question, but you forgot the `self` argument to `test()`. Also, there's no reason to use `global`. – Dan Getz Mar 24 '16 at 02:59

2 Answers2

0

You need to make test a static method (method that can be called without the class instance). You can do it with staticmethod decorator.

class TestClass():
    @staticmethod  # <---
    def test(arg1, arg2):
        global message
        print(arg1,arg2)
        message=str(arg1)+' '+str(arg2)
        print message

    def test0(self, twoword):
        global word1,word2
        word1=twoword.split('-')[0]
        word2=twoword.split('-')[1]
        print(word1,word2)
        TestClass.test(word1,word2)
falsetru
  • 357,413
  • 63
  • 732
  • 636
0

If you are looking for function overloading Python function overloading

If you just want to make this code work then add a @staticmethod above test.

class TestClass():
   @staticmethod
   def test(arg1,arg2):
       global message
       print(arg1,arg2)
       message=str(arg1)+' '+str(arg2)
       print message

   def test0( self, twoword ):
       word1=twoword.split('-')[0]
       word2=twoword.split('-')[0]
       global word1,word2
       print(word1,word2)
       TestClass.test(word1,word2)
Community
  • 1
  • 1
Vikas Madhusudana
  • 1,482
  • 1
  • 10
  • 20