Quoting from the python Language Reference[link]:
Default parameter values are evaluated when the function definition is executed.
This means that the expression is evaluated once, when the function is defined, and that that same “pre-computed” value is used for each call. This is especially important to understand when a default parameter is a mutable object, such as a list or a dictionary: if the function modifies the object (e.g. by appending an item to a list), the default value is in effect modified. This is generally not what was intended. A way around this is to use None as the default, and explicitly test for it in the body of the function, e.g. …
I was not aware of this. I have an excuse though, in previous versions of Python, language reference was described as “for language lawyers” [link]. Since I never liked lawyers, I never bothered reading it. I am happy though it was changed in current release and now it is described as “describes syntax and language elements”. I guess I have to read it now.
Below is an example for a bug I had caused by this feature and which made me aware of this issue.
>>> import time >>> def now(t=time.time()): ... print t ... >>> now() 1228988224.36 >>> now() 1228988224.36
Just came across this what might be a usefull utility which provides help within the python interperter using collaborative data gathered at http://bug.gd
tzury@regulus:~$ sudo easy_install bug.gd tzury@regulus:~$ sudo /usr/bin/python error_help_config.py tzury@regulus:~$ python Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52) [GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> 0/0 Traceback (most recent call last): File "<stdin>", line 1, in <module> ZeroDivisionError: integer division or modulo by zero >>> error_help() ========== 1 of 7 ========== Error: Traceback (most recent call last): File "<stdin>", line 1, in <module> ZeroDivisionError: integer division or modulo by zero Solution: I divided by zero as a test. Maybe you did the same thing? The trick is not to divide by zero! ... ...
Just came across this in python mailing list (link)
(then click the ‘open browser’ button)
Python-Wifi is a Python library that provides access to information about a W-Lan card’s capabilities, like the wireless extensions written in C.
>>> from pythonwifi.iwlibs import Wireless >>> wifi = Wireless('eth1') >>> wifi.getEssid() 'romanofski' >>> wifi.getMode() 'Managed'Sander Marechal
# a simple tcp server import SocketServer class EchoRequestHandler(SocketServer.BaseRequestHandler ): def setup(self): print self.client_address, 'connected!' self.request.send('hi ' + str(self.client_address) + '\n') def handle(self): data = 'dummy' while data: data = self.request.recv(1024) self.request.send(data) if data.strip() == 'bye': return def finish(self): print self.client_address, 'disconnected!' self.request.send('bye ' + str(self.client_address) + '\n') #server host is a tuple ('host', port) server = SocketServer.ThreadingTCPServer(('', 50008), EchoRequestHandler) server.serve_forever()