admin管理员组文章数量:1417070
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
4 Answers
Reset to default 5person = {
'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 dict
like 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
}
本文标签: Javascriptstyle object methods in PythonStack Overflow
版权声明:本文标题:Javascript-style object methods in Python? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745259592a2650294.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论