最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript-style object methods in Python? - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question asked Jul 27, 2011 at 21:27 user18015user18015 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. – user395760 Commented Jul 27, 2011 at 21:32
Add a ment  | 

4 Answers 4

Reset to default 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']()

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 bination 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, overing 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.

def test():
    print "hello"

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

testDict["speak"]()

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
}
发布评论

评论列表(0)

  1. 暂无评论