Home > Back-end >  how to call a method in another method in python
how to call a method in another method in python

Time:10-22

I have two methods method1 and method2. we can not call method2 alone and it should be in method1. how can i make sure method1 has return statement in this scenario.

def method1(list1, dict1):
 for i in list1:
  updated_list = i.replace(".",",") #some logic that will update the list not exactly this
  method2(updated_list, dict1)#here i want to have a return statement with updated_list but need to call method2 as per the logic. can i do return here?
 
def method2(updated_list, dict1):
 if updated_list[0] == "success":
  dict1[updated_list[0]] = updated_list
 return dict1

def main_usuage_of_two_methods(list1):
 dict1 = {}
 method1(list1, dict1) # i want to store in a variable by giving return in method1 like test = method1(list1, dict1)
 print(dict1)

CodePudding user response:

Your inline comment is correct on line 4, you could just use return method2(updated_list, dict). On a side note, you'll want to rename your variables since list and dict are already reserved keywords in the Python standard library. method1 needs to have the loop indented, and the inline comment after that should be '#' instead of '//'

CodePudding user response:

I think using a global variable will solve the problem of 'return'.
The whole idea of returning is to end the execution of that function or to return from the memory block containing the function to the main memory block.

  • Related