Skip to main content

list as default value in class __init__() - watchout for surprise

Note to self: don't use list as a default value in class methods if its going to be modified later, as it might cause some trouble.

Just realized something:

In [1]: class MyClass(object):
...: def __init__(self,data=[]):
...: = data
...: def addData(self,value):

In [2]: obj = MyClass()

In [3]: obj.addData('hello')

In [4]:
Out[4]: ['hello']

In [5]: obj2 = MyClass()

In [6]:
Out[6]: ['hello']

In [7]: obj3 = MyClass([])

In [8]:
Out[8]: []

In [12]: def myfunc(val,data=[]):
....: data.append(val)
....: return data

In [13]: myfunc(1)
Out[13]: [1]

In [14]: myfunc(2)
Out[14]: [1, 2]

In [15]: myfunc(3)
Out[15]: [1, 2, 3]

At first i thought its a bug/weirdness, but after banging my head a bit to wall and a long stare at the ceiling, it made sense.

During class definition, the list object already instantiated. Therefore, the default value of data variable is a reference to a list object, rather than a new list on each instantiation.

Not quite sure how to explain this in words ..

In [1]: class MyClass(object):
...: def __init__(self,data=[]): # data->List object at 0x0001
...: = data #>List object at 0x0001
...: def addData(self,value):
...: #>List object at 0x0001

In [2]: obj = MyClass()

In [3]: obj.addData('hello')

In [4]: #>List object at 0x0001
Out[4]: ['hello']

In [5]: obj2 = MyClass()

In [6]: #>List object at 0x0001
Out[6]: ['hello']

In [7]: obj3 = MyClass([]) # []->List object at 0x0002

In [8]: #>List object at 0x0002
Out[8]: []

I think you got the idea.

Popular posts from this blog

Adding simple popup to Plone frontpage

Here is a little guide for those who want to add a simple popup to the Plone frontpage for some purpose (eg: announcements, advertisements, etc).

Create a basic html file containing the content you want to appear in the popup. Upload it into $PLONE_SITE/portal_skins/custom (as Page Template) and for the sake of this example, name it popup.html

Afterward, create a Javascript file with your Pop-Up loader script. For example , this script:

function popup(mylink, windowname)
if (! window.focus)return true;
var href;
if (typeof(mylink) == 'string')
href=mylink.href;, windowname, 'width=220,height=400,scrollbars=no');
return false;

popup('popup.html', 'My Popup');

Also upload this file into $PLONE_SITE/portal_skins/custom (as Page Template too). For this example, name it as popup.js

Afterward, in $PLONE_SITE/portal_javascripts , add popup.js as a new script into portal_javascripts…

Tee'ing Python subprocess.Popen output

A little hack for python coders out there who wanted to have a functionality similar to the unix's tee command for redirecting output to multiple places.

import sys
from subprocess import Popen,PIPE
p = Popen(['put','command','and','arguments','here'],stdout=PIPE)

while True:
o = p.stdout.readline()
if o == '' and p.poll() != None: break
# the 'o' variable stores a line from the command's stdout
# do anything u wish with the 'o' variable here
# this loop will break once theres a blank output
# from stdout and the subprocess have ended

HOWTO: Mirroring Yum repositories using Yum-Utils

As promised before in one of my previous post, a Howto on how to mirror and manage yum repositories using some of the utilities in yum-utils.

The first step is, well, of course, is to get yum-utils from fedora repository
yum install yum-utils

Reposync is a utility for mirroring and synchronizing local copy of a yum/rpmmetadata repository.

This utility is very useful if you wanted to make a yum repository mirror. Before this, I used "wget -R -np -N" but this method is a little bit tedious and it doesnt work with repos that didn't use directory listing. Plus, it also download together additional site stuff that I don't need/want and it doesn't verify checksum of the downloaded packages.

Mirroring a repo using this utility is easy, just execute this command
reposync -r <repoid> -a <arch> -n
and the repo will be mirrored in a folder with the same name of the repoid in the directory you executed the command. Eg: you executed the command in /mnt/storage/mi…