0

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.

4
  • It still seems pretty hacky but you could probably use a try/except and have it pass on exceptions. Commented Jul 31, 2014 at 4:59
  • You need a : after def Myprint(d) Commented Jul 31, 2014 at 5:01
  • IanAuld the problem is at the moment it always triggers a exception even if you have passed in a valid function that takes 1 paramater Commented Jul 31, 2014 at 5:01
  • also you need a def before __init__(self): Commented Jul 31, 2014 at 5:02

1 Answer 1

1

You've got lots of syntax errors in your example code -- plus you're making a function (def MyClass) rather than a class (class MyClass). This is a working version of your code:

def getData():
  return [1, 2, 3, 4, 5]

class MyClass(object):

  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 handleData(self):
    data = getData()
    if self.__myFunc is not None:
      self.__myFunc(data)

def MyPrint(d):
  print(d)

m = MyClass()
m.setFunc(MyPrint)
m.handleData()

Check the differences between yours and mine -- In some cases they're pretty subtle.

Sign up to request clarification or add additional context in comments.

4 Comments

I have fixed the syntax errors, i think, python is not the normal language i use. Your solution still doesn't fix the fact that atleast for me self.__myFunc is not None always returns false in handledData() so MyPrint is never called
Definitely working for me as written on both 2.7 and 3.4. If the is not None check is always returning false, then clearly you're not actually setting the attribute correctly. Are you sure there's not a typo somewhere in your code? Also, double-underscores are generally reserved for "magic" methods, and it's bad form to make new ones if you're not using metaclasses or something else exotic. Single underscore is what you want here.
@user3479224 -- double underscores are only reserved for "magic" methods if the underscores are on both sides. e.g. __add__ is reserved magic, but __add is not. __add will invoke name mangling though. That said, I do agree that a single underscore would be more idiomatic here...
Apparently i just made a typo, and python decided to create me a new variable rather then telling my it didn't exist, but ill give you the answer for taking the time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.