From 8997c760a2cdfdc4a78e093c143788938fa68407 Mon Sep 17 00:00:00 2001 From: Chris Glass Date: Wed, 25 Jun 2014 17:43:45 +0200 Subject: [PATCH] Do not use mutables as default values! This blog article explains why (just an example, many other articles discuss this ad nauseam): http://pythonconquerstheuniverse.wordpress.com/2012/02/15/mutable-default-arguments/ --- electrum | 7 +++---- lib/blockchain.py | 4 +++- lib/daemon.py | 4 +++- lib/network.py | 4 +++- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/electrum b/electrum index 15edf601a..e63803db8 100755 --- a/electrum +++ b/electrum @@ -103,16 +103,15 @@ def print_help_cb(self, opt, value, parser): print_help(parser) -def run_command(cmd, password=None, args=[]): - import socket +def run_command(cmd, password=None, args=None): + if args is None: + args = [] # Do not use mutables as default values! if cmd.requires_network and not options.offline: network = NetworkProxy(config) if not network.start(start_daemon= (True if cmd.name!='daemon' else False)): print "Daemon not running" sys.exit(1) - - if wallet: wallet.start_threads(network) wallet.update() diff --git a/lib/blockchain.py b/lib/blockchain.py index 81b940558..ac5ea1489 100644 --- a/lib/blockchain.py +++ b/lib/blockchain.py @@ -241,7 +241,9 @@ class Blockchain(threading.Thread): return h - def get_target(self, index, chain=[]): + def get_target(self, index, chain=None): + if chain is None: + chain = [] # Do not use mutables as default values! max_target = 0x00000000FFFF0000000000000000000000000000000000000000000000000000 if index == 0: return 0x1d00ffff, max_target diff --git a/lib/daemon.py b/lib/daemon.py index 8ed0a3644..f34159e92 100644 --- a/lib/daemon.py +++ b/lib/daemon.py @@ -34,7 +34,9 @@ class NetworkProxy(threading.Thread): # connects to daemon # sends requests, runs callbacks - def __init__(self, config = {}): + def __init__(self, config=None): + if config is None: + config = {} # Do not use mutables as default arguments! threading.Thread.__init__(self) self.daemon = True self.config = SimpleConfig(config) if type(config) == type({}) else config diff --git a/lib/network.py b/lib/network.py index a6be0a435..f07846aa0 100644 --- a/lib/network.py +++ b/lib/network.py @@ -72,7 +72,9 @@ from simple_config import SimpleConfig class Network(threading.Thread): - def __init__(self, config = {}): + def __init__(self, config=None): + if config is None: + config = {} # Do not use mutables as default values! threading.Thread.__init__(self) self.daemon = True self.config = SimpleConfig(config) if type(config) == type({}) else config