Apparently i can make typo's and that was the cause of this question Not used to dynamically typed languages and there lack of complaining
I am trying to create a python class that will have a member that can be set to a function pointer taking 1 parameter.
Originally i tryed (Simplified example)
class MyClass():
def __init__(self):
self.__myFunc = None
# func is a pointer to a function that takes 1 paramater
def setFunc(self, func):
self.__myFunc = func
def getData(self):
return 42
def handleData(self):
data = self.getData()
if self.__myFunc is not None:
self.__myFunc(data)
# Separate function not in class
def MyPrint(d)
print(d)
m = MyClass()
m.setFunc(MyPrint)
m.handleData()
The first problem i had was self.__myFunc is not None in the if test always returned False so i removed that temporarily
The second issue i had was the line self.__myFunc(data) caused a TypeError: 'NoneType' object is not callable after following empty function object in python i changed self.__myFunc = None to self.__myFunc = lambda: None
The change to fix the second issue caused a TypeError: <lambda>() takes 0 positional arguments but 1 was given
Ideally i would like to do this without creating a new function that takes a parameter and does nothing because that seems very hacky.
try/exceptand have itpasson exceptions.def Myprint(d)defbefore__init__(self):