怎么简单继承第三方库?

继承库,不是类!

import uiautomator2
class expand(uiautomator2):
    def unlock(self):
        d.screen_on()
        for i in range(23):
            if d.info['currentPackageName'] in ['com.android.systemui', 'android']:  #
                logging.info('解锁屏幕')
                d.swipe_ext("up", scale=0.9)
                d.unlock()
            else:
                logging.info('已解锁')
                return True
            time.sleep(1)

TypeError: module() takes at most 2 arguments (3 given)
会出错!我想给库加个功能,不想修改库,不然每次升级都要去修改!
找遍了,都是说怎么继承类的

Jason990420
最佳答案

簡單的作法如下

class A():
    def __init__(self):
        pass
    def unlock(self):
        print("Old unlock")

class B(A):
    pass

class C(A):
    def unlock(self):
        print("New unlock")
        super().unlock()

d = C()
d.unlock()
New unlock
Old unlock
2年前 评论
heavenm (楼主) 2年前
heavenm (楼主) 2年前
Jason990420 (作者) 2年前
讨论数量: 15
import uiautomator2 as u2
import logging
import time
def connect(addr=None):
    global d
    d = u2.connect(addr)
    d.unlock=unlock
    return d

def unlock():
    d.screen_on()
    for i in range(23):
        if d.info['currentPackageName'] in ['com.android.systemui', 'android']:  #
            logging.info('解锁屏幕')
            d.swipe_ext("up", scale=0.9)
            d.unlock()
        else:
            logging.info('已解锁')
            return True
        time.sleep(1)

想到一个方法,新建一个init.py
然后导入,就可以了
不过这个方法应该是有问题的
init ][ unlock ][ INFO ][ 14 ][ 解锁屏幕
init ][ unlock ][ INFO ][ 14 ][ 解锁屏幕
init ][ unlock ][ INFO ][ 18 ][ 已解锁
init ][ unlock ][ INFO ][ 18 ][ 已解锁
init ][ unlock ][ INFO ][ 18 ][ 已解锁
这些都只出现一次的应该!

2年前 评论
Jason990420

觉得这个问题的标题不对 ?!

下面这个例子如何 ?

>>> import uiautomator2 as u2
>>>
>>> class Device(u2._Device, u2._AppMixIn, u2._PluginMixIn, u2._InputMethodMixIn, u2._DeprecatedMixIn):
...     def unlock(self):
...         print('New unlock')
...         return    # function stop here just for verification !!!
...         self.screen_on()
...         for i in range(23):
...             if self.info['currentPackageName'] in ['com.android.systemui', 'android']:  #
...                 logging.info('解锁屏幕')
...                 self.swipe_ext("up", scale=0.9)
...                 # super().unlock()    # call which `unlock` ? 
...             else:
...                 logging.info('已解锁')
...                 return True
...             time.sleep(1)
...
>>> u2.Device = Device
>>>
>>> device = u2.connect("http://127.0.0.1:8080")
>>> device.unlock()
New unlock
2年前 评论
heavenm (楼主) 2年前
Jason990420 (作者) 2年前
heavenm (楼主) 2年前
heavenm (楼主) 2年前
Jason990420 (作者) 2年前
heavenm (楼主) 2年前
Jason990420 (作者) 2年前
heavenm (楼主) 2年前
heavenm (楼主) 2年前
Jason990420

簡單的作法如下

class A():
    def __init__(self):
        pass
    def unlock(self):
        print("Old unlock")

class B(A):
    pass

class C(A):
    def unlock(self):
        print("New unlock")
        super().unlock()

d = C()
d.unlock()
New unlock
Old unlock
2年前 评论
heavenm (楼主) 2年前
heavenm (楼主) 2年前
Jason990420 (作者) 2年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!