On Wed, 09 Nov 2005 13:46:52 +0100, Yves Glodt wrote: > My question is: Is there no way to append to a non existing list? > > I am lazy for declaring it first, IMHO it bloats the code, and (don't > know if it's good to say that here) where I come from (php) I was used > to not-needing it...
But what happens if the non-existent list you try to append to is actually a non-existent dict, or worse, a non-existent float? You'll get errors and maybe even crash your computer and wipe the hard disk clear. *wink* How is appending to a non-existent object supposed to work? something.append(0) What object does the name "something" refer to? Does it even have an append method? How can you tell what methods it has if it doesn't exist yet? No, you can't append to a non-existent list, just as you can't add two non-existent numbers together. If PHP allows you to append to non-existent lists, then it is a poor design decision, and it obviously only works because PHP is much more limited than Python. Consider the following code: class PList(list): def append(self, obj): print "Appending object to PList" self.__class__.append(self, obj) # call the superclass class QList(list): def append(self, obj): print "QList append is being called" self.__class__.append(self, obj) class RList(QList): def append(self, obj): self.__class__.append(self, obj) print "Now calling RList append" something.append("hello world") # call append on a non-existent object What should Python do? Should "something" be a RList, QList, PList or ordinary list? -- Steven. -- http://mail.python.org/mailman/listinfo/python-list