Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Call method from another method within a class
Not sure if this is the right thinking but anyway, this is what I'm trying to do:

I have a Class which includes some variables and some methods. The class has itself some code thats executed where instansiating an object 'a('Call a method')'. I can call method a() but when method a() calls method b() it all fails.

Whats the reason for this and how should it be solved?
It's when the call b('aa') executes I get the error: 'NameError: global name 'b' is not defined'
Why can't method a() reach method b()?

class TestClass:

    def __init__(self):

    def b(name):
        print '---b---', name

    def a(name):
        print '---a---', name

    a('Call a-method')
buran wrote Sep-11-2019, 12:15 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
it will work, but you should use python 3.7.4
class TestClass:
    def __init__(self, name):

    def methodb(self, name):
    def methoda(self, name):
def tryit():
    TestClass("Call a-method")

if __name__ == "__main__":
Thanks! Tried to tag the python code according to the instructions.

I modified your code slightly so that it works in Python 2.7
The difference is that I want to call method a from within the class. As you see in my firsst example, the
intendation of the call "a('Call a-method')" is inside the class, not outside. My first example works if I comment line #13 (b('aa')).

class TestClass:
    def __init__(self, name):

    def methodb(self, name):

    def methoda(self, name):

    self.methoda('Call a-method from within class') # This doesn't.

TestClass("Call a-method") # This works
python 2, see:

You can't do this in a class
self.methoda('Call a-method from within class') # This doesn't.
you must use method

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  threading for method outside class anna 0 47 Nov-17-2019, 07:05 AM
Last Post: anna
  How to call COM-method using comtypes jespersahner 0 53 Nov-15-2019, 12:54 PM
Last Post: jespersahner
  function vs method prateekshaw 2 90 Nov-14-2019, 07:00 PM
Last Post: DeaD_EyE
  how can I changing a base class method voidptr 2 77 Nov-10-2019, 10:53 PM
Last Post: voidptr
  Polymorphism not working with a call to a abstract method colt 3 103 Nov-04-2019, 11:04 PM
Last Post: colt
  How to Call a method of class having no argument dataplumber 7 337 Oct-31-2019, 01:52 PM
Last Post: dataplumber
  print all method and property of list object engmoh 4 270 Oct-26-2019, 05:33 PM
Last Post: engmoh
  creating a variable in a method and using in another method ridgerunnersjw 5 259 Oct-04-2019, 04:03 PM
Last Post: buran
  Vernam encryption method for files JohnCTX 1 125 Sep-18-2019, 04:31 PM
Last Post: JohnCTX
  How to run a method on an argument in a formatted string Exsul 1 90 Aug-30-2019, 01:57 AM
Last Post: Exsul

Forum Jump:

Users browsing this thread: 1 Guest(s)