Python annoyance
Thursday, January 29th, 2009What really annoys me about python lists (and tuples) is that their ‘append’ method returns ‘None’. This effectively renders the append method useless when working with custom classes that offer a dict like interface via __getitem__ and __setitem__.
Take the following code for example.
class MyClass( object ):
def __init__( self ):
self.dict = {}
def __getitem__( self, name ):
return self.dict[name]
def __setitem__( self, name, value ):
self.dict[name] = value
myclass = MyClass()
myclass["list"] = ["foo"]
myclass["list"].append( "bar" )
You’d think that myclass[”list”] now contains [”foo”, “bar”]. It actually contains None (the append methods return value).