Search code examples
pythondefault

How to overwrite a default value for argument in Python?


I import a class from a library and this class uses a variable/constant which is defined inside the library but outside the class. Is it possible for me to change that value?

I probably need to change HTTP_TIMEOUT = 30 to something else.

HTTP_TIMEOUT = 30
      
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):

Solution

  • As the value of HTTP_TIMEOUT just serves as default to the timeout parameter of the RawProxy.__init__ method, you can simply specify your desired timeout as argument to the class instantiation, for example:

    p = RawProxy(timeout=60)