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
  Import Module and Method ( dynamic ) ? JohnnyCoffee 0 74 Mar-23-2020, 04:23 PM
Last Post: JohnnyCoffee
  Dynamic method ? JohnnyCoffee 2 158 Mar-20-2020, 02:40 PM
Last Post: JohnnyCoffee
  Accessing method as function object ClassicalSoul 2 171 Feb-14-2020, 09:31 PM
Last Post: wavic
  Is it mandatory to call superclass init inside the class init? psolar 3 173 Feb-14-2020, 09:16 PM
Last Post: wavic
  Problem with adding arbitrary parrameters to __init__ method sebastianvdn 1 131 Feb-03-2020, 09:30 PM
Last Post: micseydel
  TypeError: type str doesn't define __round__ method emmapaw24 7 546 Feb-03-2020, 08:38 PM
Last Post: snippsat
  calling a method for a docstring Skaperen 8 203 Jan-27-2020, 01:16 AM
Last Post: Skaperen
  threading for method outside class anna 0 124 Nov-17-2019, 07:05 AM
Last Post: anna
  How to call COM-method using comtypes jespersahner 0 142 Nov-15-2019, 12:54 PM
Last Post: jespersahner
  function vs method prateekshaw 2 192 Nov-14-2019, 07:00 PM
Last Post: DeaD_EyE

Forum Jump:

Users browsing this thread: 2 Guest(s)