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
  Return boolean from recursive class method medatib531 6 96 Yesterday, 04:27 AM
Last Post: medatib531
  python/winrt Bluetooth.GenericAttributProfile CreateAsync(GUID) method throws Not Imp pbvinoth 0 139 Jul-08-2020, 04:27 AM
Last Post: pbvinoth
  referencing another method in a class Skaperen 6 272 Jul-02-2020, 04:30 AM
Last Post: Skaperen
  how to get around recursive method call Skaperen 10 579 Jul-01-2020, 10:09 PM
Last Post: Skaperen
  How to emit a signal from another Class Method? SKDN 1 286 Jun-30-2020, 05:44 PM
Last Post: Yoriz
  get method not counting number of strings in dictionary LearningTocode 2 162 Jun-13-2020, 11:17 PM
Last Post: LearningTocode
  function/method help myv5285 3 396 May-17-2020, 04:19 AM
Last Post: buran
  TypeError: ENCODE Method, str instead of byte Rajath 1 197 May-09-2020, 06:05 PM
Last Post: bowlofred
  Error: Nested method ? JohnnyCoffee 5 399 May-03-2020, 02:43 PM
Last Post: JohnnyCoffee
  calling on a method from one class into another class which is not a child NABA 5 228 Apr-29-2020, 07:49 PM
Last Post: deanhystad

Forum Jump:

Users browsing this thread: 1 Guest(s)