android - Python class to inherit MonkeyDevice -
i followed solution given here , added method extend functionality device class. how inherit monkeydevice?
i error object has no attribute 'test'. looks class instance of type monkeydevice. doing wrong?
from com.android.monkeyrunner import monkeyrunner, monkeydevice, monkeyimage class device(monkeydevice): def __new__(self): return monkeyrunner.waitforconnection(10) def __init__(self): monkeydevice.__init__(self) def test(): print "this test" device = device() device.test(self)
you doing lot of things wrong. unfortunately don't use monkeyrunner
can't details related library itself.
what code following:
>>> class monkeyrunner(object): pass ... >>> class device(monkeyrunner): ... def __new__(self): ... return monkeyrunner() ... def __init__(self): ... super(device, self).__init__() ... def test(): ... print "this test" ... >>> device = device() >>> device.test(self) traceback (most recent call last): file "<stdin>", line 1, in <module> attributeerror: 'monkeyrunner' object has no attribute 'test' >>> device <__main__.monkeyrunner object @ 0xb743fb0c> >>> isinstance(device, device) false
note how device
not device
instance. reason __new__
method not returning device
instance, monkeyrunner
instance. answer linked in question states:
anyway achieve want should create class custom
__new__
rather__init__
,monkeydevice
instance factory and inject stuff instance or it's class/bases/etc.
which means should have done like:
>>> class device(monkeyrunner): ... def __new__(self): ... inst = monkeyrunner() ... inst.test = device.test ... return inst ... @staticmethod ... def test(): ... print "i'm test" ... >>> device = device() >>> device.test() i'm test
however isn't useful @ all, since device
function:
>>> def device(): ... def test(): ... print "i'm test" ... inst = monkeyrunner() ... inst.test = test ... return inst ... >>> device = device() >>> device.test() i'm test
afaik cannot subclass monkeyrunner
, create instance waitforconnection
method, @ least if waitforconnection
staticmethod
.
what i'd use delegation:
class device(object): def __init__(self): self._device = monkeyrunner.waitforconnection(10) def __getattr__(self, attr): return getattr(self._device, attr) def test(self): print "i'm test"
Comments
Post a Comment