Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Call method from another method within a class
#1
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):
        pass

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

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

    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.
Quote
#2
it will work, but you should use python 3.7.4
class TestClass:
    def __init__(self, name):
        self.methoda(name)

    def methodb(self, name):
        print(f"---b---{name}")
 
    def methoda(self, name):
        print(f"---a---{name}")
        self.methodb("aa")
 
def tryit():
    TestClass("Call a-method")
    

if __name__ == "__main__":
    tryit()
Quote
#3
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):
        self.methoda(name)

    def methodb(self, name):
        print("---b---{}".format(name))

    def methoda(self, name):
        print("---a---{}".format(name))
        self.methodb("aa")

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

TestClass("Call a-method") # This works
Quote
#4
python 2, see: https://pythonclock.org/

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

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  creating a variable in a method and using in another method ridgerunnersjw 5 215 Oct-04-2019, 04:03 PM
Last Post: buran
  Vernam encryption method for files JohnCTX 1 110 Sep-18-2019, 04:31 PM
Last Post: JohnCTX
  How to run a method on an argument in a formatted string Exsul 1 73 Aug-30-2019, 01:57 AM
Last Post: Exsul
  Pass Dictionary to Method is not possible Frank123456 1 140 Aug-19-2019, 10:18 AM
Last Post: buran
  Bound method Uchikago 1 213 Jul-26-2019, 04:43 PM
Last Post: Gribouillis
  Asyncio StreamReader read method doesn't respect timeout when using SSL dukadahake 0 183 Jul-24-2019, 11:55 AM
Last Post: dukadahake
  Best method: Python script called from another app, package as complete executable ironfelix717 2 184 Jul-24-2019, 07:39 AM
Last Post: DeaD_EyE
  How to create an autocomplete method that search an existing trie in python nanoprogrammer 0 179 Jul-20-2019, 12:09 AM
Last Post: nanoprogrammer
  How to intercept python method calls? DineshBhat 0 128 Jul-15-2019, 03:46 AM
Last Post: DineshBhat
  What is the use of call method and when to use it? everyday1 1 169 Jul-14-2019, 01:02 PM
Last Post: ichabod801

Forum Jump:


Users browsing this thread: 1 Guest(s)