I am testing a small functionality where the manage()
function within barbar
invokes call_central
, which has functions call2 and call3.
I would like an object on call_central
so that I can invoke call2
and call3
where needed.
Some background: What I intend to do with this is. In my program, call2 is instantiating another class, and call3 is using this information to perform various other operations.
EDIT: More details: my basis to create nested functions is as follows. Let's assume you are trying to do some scheduling work and testing various schedulers. The barbar function will get all the parameters from "all" the scheduling algorithms you are going to implement. Each scheduling algorithm has class in itself. In this MWE, it is CallThisClass. What "call_central" does is instanting this particular scheduling algorithm in call2 and using it to actually schedule in call3
A few questions:
- Why does it ask me to pass "self" as an argument.
- Why does
s(self).call2()
returnNone
- Is there a way to avoid using nested functions for this?
Code:
#!/usr/bin/env python3
class CallThisClass:
def __init__(self):
pass
def do_something_here(self):
while True:
print ("doing something")
class CallThisClass1:
def __init__(self):
pass
def do_something_here(self):
while True:
print ("doing something")
class barbar:
def __init__(self, which_function=None):
print ("do nothing")
self.which_function = which_function
self.manage()
def manage(self):
FUNCTIONS = globals()['barbar']
s = getattr(FUNCTIONS, self.which_function)
print (s(self))#.call2())
def call_central(self):
print ("in call central")
def call2():
self.invoke_callthisclass = CallThisClass()
print ("do nothing from call2")
def call3():
self.invoke_callthisclass.do_something_here()
print ("do nothing from call3")
return 0
def call_central2(self):
print ("in call central")
def call4():
self.invoke_callthisclass1 = CallThisClass1()
print ("do nothing from call4")
def call5():
self.invoke_callthisclass1.do_something_here()
print ("do nothing from call5")
return 0
d = barbar(which_function="call_central")