######################## test __getattr__()class Hi2():def __init__(self, name):self.name=namedef __call__(self):print 'Hi2.%s() was called'% (self.name)
class Hi(object):def __getattr__(self, key):
print 'in Hi.__getattr__(%s)' % keyreturn Hi2(key)hi = Hi()hi.there #member referencehi.there() #method call
in Hi.__getattr__(there)in Hi.__getattr__(there)Hi2.there() was called
in Hi.__getattr__(there)Unknown exception: [object Error]Traceback:test_getattr.py, line 17:hi.there()
This is expected behavior though?
As your example demonstrates, pyjs does not currently support __call__. This doesn't mean it isn't handling the __getattr__ call; ultimately it's failing because you're trying to call an instance (tho I can't comment offhand why you only see one print) . Try putting a print in the __init__ def of the proxied class, and I believe you will see it created twice. Also, replace __call__() with call(), then invoke with hi.there.call()... I'm pretty sure that will work as well.
If you take a look at the generated JavaScript you will see most attribute access is transformed into getattr() calls; also take a look at translater_proto.py if you want to explore further, because that is where getattr (and most other AST magic) is actually handled.
would be nice to have __call__ though...
--
C Anthony [mobile]
######################## test __getattr__() (modified as C. Anthony sugguested)
class Hi2():def __init__(self, name):
print 'in Hi2.__init__(%s)' % nameself.name=namedef call(self):
print 'Hi2.%s() was called'% (self.name)class Hi(object):def __getattr__(self, key):print 'in Hi.__getattr__(%s)' % keyreturn Hi2(key)hi = Hi()hi.there
hi.there.call()
in Hi.__getattr__(there)in Hi2.__init__(there)in Hi.__getattr__(there)in Hi2.__init__(there)Hi2.there() was called
in Hi.__getattr__(there)in Hi2.__init__(there)
Unknown exception: [object Error]Traceback:
test_getattr.py, line 18:hi.there.call()
hi = Hi()hi.therehi.there.call()
hi.there()
$m['hi'] = $m['Hi']();$p['getattr']($m['hi'], 'there');$m['hi']['there']['$$call']();$m['hi']['there']();
$m['hi'] = $m['Hi']();$p['getattr']($m['hi'], 'there');$p['getattr']($m['hi'], 'there')['$$call']();
$p['getattr']($m['hi'], 'there')();