In Javascript, I can do this:
var person = {
name: 'Joe',
age: '35',
speak: funct开发者_运维知识库ion(){
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?
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 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 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
}
精彩评论