Python:有没有办法在导入的库中更改值?

时间:2019-08-29 10:14:13

标签: python

如果我从库中导入一个类,并且该类使用了在库内部定义但在类外部定义的变量/常量,那么我是否可以更改该值?

例如我正在编写脚本:

from bitcoin.rpc import RawProxy
p = RawProxy()
hash = '0000000000000001b6b9a13b095e96db41c4a928b97ef2d944a9b31b2cc7bdc4'        
infoblock = p.getblock(hash)
print(infoblock['difficulty'])

我可能需要将HTTP_TIMEOUT = 30更改为其他内容-从库rpc.py

...
HTTP_TIMEOUT = 30

class JSONRPCException(Exception):
    def __init__(self, rpc_error):
        super(JSONRPCException, self).__init__('msg: %r  code: %r' %
                (rpc_error['message'], rpc_error['code']))
        self.error = rpc_error


class RawProxy(object):
    # FIXME: need a CChainParams rather than hard-coded service_port
    def __init__(self, service_url=None,
                       service_port=8332,
                       btc_conf_file=None,
                       timeout=HTTP_TIMEOUT,
                       _connection=None):
....

1 个答案:

答案 0 :(得分:1)

由于HTTP_TIMEOUT的值仅作为timeout方法的RawProxy.__init__参数的默认值,因此您可以简单地将所需的超时指定为类实例化的参数,例如:

p = RawProxy(timeout=60)