1

Ok, So I am seeing something weird and I think I misunderstood how to pass functions around in python..

class Foo:
  def execute(self, **kwargs):
    print "self is ", self # <foo.Foo object at 0x1012948d0>
    features = {func_name: feature_func(kwargs, arg=arg)for func_name, 
                              feature_func in Foo.ALGORITHMS.items()}

  def func1(self, **kwargs):
     print "self is " , self # prints {}
     self._funchelp()

  def __funchelp(self):
     # pass


  ALGORITHMS = {'func1': func1}

So, what I am not getting is why is self printing to be an empty dictionary??

features = {func_name: feature_func(kwargs, arg=arg)for func_name, 
                              feature_func in Foo.ALGORITHMS.items()}

Something gets messed up above

And then

self._funchelp() Returns AttributeError: 'dict' object has no attribute '_Foo__funchelp'

1 Answer 1

1

It's probably because you need to include self as the first argument to feature_func in your dict comprehension. Also, you need to put the ** in front of kwargs. You may also need to set the 'arg' key on kwargs as follows to avoid some kind of argument error. Not sure about that though:

kwargs['arg'] = 123
features = {func_name: feature_func(self, **kwargs) for func_name, 
                          feature_func in Foo.ALGORITHMS.items()}

The weird error is probably because you're trying to access _funchelp as an attribute on an empty dict object. Furthermore, your function is defined as __funchelp with two underscores, which could get mangled by python because of the way it handles double underscore attributes internally. I think _funchelp must be a typo because I don't think it would be mangling the name if that was really what was in your code.

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

1 Comment

this is correct, you're not instantiating an instance of the class. either make classmethods or instantiate an instance.

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.