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()?

1
2
3
4
5
6
7
8
9
10
11
12
13
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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')).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
1
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
  Accessing method attributes of python class Abedin 6 941 Apr-14-2025, 07:02 AM
Last Post: buran
  __eq__ method related problem Tsotne 6 945 Mar-09-2025, 03:48 PM
Last Post: Tsotne
  Changing client.get() method type based on size of data... dl0dth 1 739 Jan-02-2025, 08:30 PM
Last Post: dl0dth
  Error in the method "count" for tuple fff 2 860 Nov-24-2024, 03:29 AM
Last Post: fff
  Trying to understand method Giri234 1 744 Oct-04-2024, 02:10 AM
Last Post: deanhystad
  Destructor method adding in string chizzy101010 3 971 Sep-03-2024, 12:31 PM
Last Post: chizzy101010
  Difference between method and attribute holding a function ulrich 2 956 Jun-30-2024, 08:35 AM
Last Post: snippsat
  comtypes: how to provinde a list of string to a COM method zalanthas 0 981 Jun-26-2024, 01:27 PM
Last Post: zalanthas
Question No disconnect method for snowflake.connector ? Calab 0 853 Jun-11-2024, 09:42 PM
Last Post: Calab
  Which method name would you choose? Gribouillis 7 1,844 May-30-2024, 07:05 AM
Last Post: buran

Forum Jump:

User Panel Messages

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