Previous Article:
Bonsai Bugs and pyblosxom update
Related Articles:
"Older than dirt"
Happy New Year!
Opening files in Python
Python Code Formatting Conventions
Today, for only the second time ever, I’ve been badly stung by Python’s behaviour (I’ve hit other language behaviour problems before but never any that held me up for as long). This sounds ok – after all, I’ve been coding in the language since 1999 (quick detour: I was in the middle of my final year project and had to parse and reformat large datasets. With no time to learn Perl and knowing that there must be a better way than C++/Java, I downloaded Python – a language I had read much about but never used. In no time at all, the data was parsed and I’ve been using Python more and more ever since).
Unfortunately, I have now been bitten twice by the same behaviour – mutable objects (I suspect this isn’t the correct name for this). The code comprised quite simply:
my_new_map = a_n_other_map.fromkeys(
a_n_other_dict.keys(), [])
Obvious, isn’t it? I wanted to create a new map with members initialised to empty lists. I proceeded to use my_new_map and insert values at will into my “lists”.
“Lists”? I should be so lucky, each of the entries in my_new_map shared the single list I had passed to fromkeys originally. I ended up with the same information in all lists…I spent a lot of time rechecking all of my code until I finally spotted this.
I should have known better, but I was in a hurry and it was very tempting to write this code. I finally ended up with:
my_new_map = a_n_other_map.fromkeys(
a_n_other_dict.keys(), None)
for ky in my_new_map.keys():
my_new_map[ky] = []
Not nearly as pretty – but it definitely works! Note: fromkeys actually defaults to None, I’ve included it here for clarity.
On reflection, perhaps it isn’t such a disaster that I’ve been bitten by this “bug”. fromkeys was only introduced in Version 2.3 of Python. One thing is for sure: I won’t make this same mistake a third time!
For a list of other issues that are likely to jump up and bite you if you are new to Python, I recommend reading Hans Novak’s Python Pitfalls which I first read when a link appeared on that indispensible resource The Daily Python URL From Hans’ page I see Andrew Kuchling’s Python Warts and Steve Ferg’s Python Gotchas Both of these are also worth a look.
* * *