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.