I first learned python at version 1.4 or so, and I am supporting python code that was also written around that time.
Since Python 1.5 there have been many improvements to the language, and I have religiously read the release notes of every new release in an attempt to stay up to date. My success was mixed, and I occasionally come across better ways to do things that were introduced in later Python releases, that passed me by somehow.
I plan to share them with you here….
Lets start with the ubiquitous dict
Dictionaries are wonderful things that can be used in all sorts of ways. Having a built a dictionary of values though, you will eventually need to retrieve data from it. Here is the first trap for an older user, I always want to write something like:
if dictionary.has_key(key):
data = dictionary[key]
….
This has of course been superseded and nowadays one writes:
if key in dictionary:
data = dictionary[key]
….
Note that if you want to return a default value if the key is not in the dict, you can use the get() method.
When iterating over a dictionary it’s a similar story:
for key in dictionary.keys():
…..
has been improved an you can use:
for key in dictionary:
……..
Naturally if you want to change the dictionary inside the for loop, you will have to use the keys() method to obtain a list to iterate over, just like in the old days!
I hope you found that useful …
Pricks and Mortar
Well its been sometime, hasn’t it.
I bet you thought that I’d got bored and gone away to new and more interesting things.
Well you thought wrong!
In fact I’ve been busy moving house, decorating, laying floors, and all those other knee destroying things that go with having a bank as your landlord.
After 9 months in the new place what do I have:
Why do people buy houses? Its a supreme act of idiocy dammit!
Security I hear you say. Bullshit! A landlord is more likely to listen to your entreaties than a bank manager who knows exactly how deeply in the financial shit you are, and can look at your financial status with a clickety click of the keyboard.
Investment I hear you say. Bullshit! If I invest my hard earned cash in unit trusts, I get at least 9% pa. If I invest in a house, I pay 5% pa + repairs on the house + insurance in case the damn thing burns down. I wont even go into the costs of mad women with credit cards furnishing new houses here, as it may severely damage my bedroom rights.
Why do we buy houses then? Well I think its some instinct that comes from the time when there were no bank managers, and the size of your cave was used to attract crumpet. (Incidentally I think cars as an investment are just as stupid, but I’ll go into that later.)
Oh well, do as I say, not as I do,
It all goes to show I’m as big a prick as the next man….