Blog

I've finally gotten around to updating andrew.puzzling.org to have a just-barely-passable stylesheet, so hopefully I'll feel a little more inclined to put content here regularly.

Python's __new__

I ran into an annoying issue with Python's __new__ magic method recently. Essentially, I wanted to modify a class so that attempting to construct it more than once would return the existing instance, rather than construct another one, to avoid problems caused by side-effects of its __init__. So, something very much like a Singleton (no, not the one with the sundial). I thought that a minimally disruptive way to do this would be to create a custom __new__ that returned an existing instance when available, otherwise just call object.__new__ as normal.

Unfortunately, Python invokes __init__ from type.__call__, not object.__new__ as I had thought. Of course, calling __init__ is exactly what I wanted to avoid, so I had to do something else instead. Gar.

Actually, what really happens is even more complicated: for instance if you return something from SomeClass.__new__ that is not an instance of SomeClass, it won't call __init__. There's not much documentation on this stuff... there's bits and pieces in this “incomplete draft”, but other than that you seem to be stuck with the source (see the type_call function if you're curious) — or just experimenting until you find something that works.

Wikitravel

Lots of open source conferences are organised by volunteer work, and a lot of effort goes into doing it. Then, once the conference is over, all the research the volunteers did on mundane but extremely useful things, like accomodation and ways to get there, rots on some obsolete web page no-one ever looks at again. In my ideal world, conferences like linux.conf.au and PyCon would be contributing that research to Wikitravel, so that more people (and future conferences!) can benefit. The Washington (D.C.) page on Wikitravel is a great example — it's got a fair bit of information, but is almost totally devoid of places to sleep.

For this reason, I contributed some stuff to the MatarĂ³ page while I was there for the Ubuntu conference.

Last modified: 13 January 2005

Powered by backwards