Python Forum
Call method from another method within a class
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')
Reply
#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()
Reply
#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
Reply
#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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Int.From_Bytes Method - Conversion of Non-Escaped Bytes Objects new_coder_231013 2 380 Apr-06-2024, 04:24 PM
Last Post: new_coder_231013
  Why is the copy method name in python list copy and not `__copy__`? YouHoGeon 2 241 Apr-04-2024, 01:18 AM
Last Post: YouHoGeon
  class definition and problem with a method HerrAyas 2 225 Apr-01-2024, 03:34 PM
Last Post: HerrAyas
  super() and order of running method in class inheritance akbarza 7 721 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
  problem usage of static method akbarza 5 530 Feb-03-2024, 07:43 AM
Last Post: paul18fr
  Building a DoublyLinkedList in Python - - append method Drone4four 2 414 Jan-08-2024, 01:27 PM
Last Post: Drone4four
Information Best distribution method inovermyhead100 0 550 Jul-19-2023, 07:39 AM
Last Post: inovermyhead100
  method call help sollarriiii 6 1,142 Feb-21-2023, 03:19 AM
Last Post: noisefloor
  That compression method is not supported tester_V 9 3,479 Jan-31-2023, 07:05 PM
Last Post: tester_V
  sys.argv method nngokturk 3 1,081 Jan-23-2023, 10:41 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020