2

In Javascript, I can do this:

var person = {
  name: 'Joe',
  age:  '35',
  speak: function(){
    return 'o hai i am joe'
  }
}

Then I can call that method:

person.speak()
'o hai i am joe'

I know this can be done with classes in Python, and presumably that's the right way to go.

Nonetheless, I'm curious -- is there some way to add a function as a value in a Python dictionary?

1
  • 1
    The javascript version does more than that, it defines a whole object. If you want real objects in Python, not just lumped-together values and functions (which don't have access to th latter), you'll have to write classes. Commented Jul 27, 2011 at 21:32

4 Answers 4

5
person = {
  'name': 'Joe',
  'age':  '35',
  'speak': lambda: 'o hai i am joe',
}

However, in Python (unlike JavaScript), attribute and [] access are different. To execute speak, write

person['speak']()
Sign up to request clarification or add additional context in comments.

Comments

3

One of the key differences between javascript and python is the handling of the target object in the method's namespace. In javascript, this is set as needed when the method is called, but in python, self is determined in a combination of class creation time (turning functions into instancemethods) and when the attribute is accessed (binding the im_self property on the instancemethod). Even if you were to use attribute access only, overcoming this difference is a bit tricky when you want to bind instance methods to individual instances, instead of the class.

import functools
class JSObject(object):
    def __getattribute__(self, attr):
        """
        if the attribute is on the instance, and the target of that is
        callable, bind it to self, otherwise defer to the default getattr.
        """
        self_dict = object.__getattribute__(self, '__dict__')
        if attr in self_dict and callable(self_dict[attr]):
            return functools.partial(self_dict[attr], self)
        else:
            return object.__getattribute__(self, attr)

Here it is in action:

>>> foo = JSObject()
>>> foo.bar = 'baz'
>>> foo.bar
'baz'
>>> foo.quux = lambda self: self.bar
>>> foo.quux
<functools.partial object at 0x7f8bae264ba8>
>>> foo.quux()
'baz'

Making the above class a bit more dictlike is a separate issue, and in my opinion, not the best "feature" of JavaScript to emulate, but supposing we wanted that "anyways", we would probably start by subclassing dict, and once again overloading __getattr__ or __getattribute__, which I will leave as an exercise.

1 Comment

This is fascinating, thanks. I'd never looked at functools.
2
def test():
    print "hello"

testDict = {"name" : "Joe", "age" : 35, "speak" : test}

testDict["speak"]()

Comments

0

You can, either by defining the functions ahead of time:

def speak_function(txt):
  print txt

person = {
  'speak': speak_function
}

Or possibly by using lambdas (in the case of simple returns):

person = {
  'speak': lambda x: x
}

1 Comment

return is not valid in lambdas, its implicit.

Your Answer

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