append to non-existing list

skip at skip at
Wed Nov 9 14:15:31 CET 2005

    Yves> My question is: Is there no way to append to a non existing list?

My question in return is: How is Python supposed to know that pkcolumns is
supposed to be a list instead of some other type of object that happens to
define an append() method?  For example, my code might contain this class
definition before the suspect pkcolumns.append() call:

class MaxLengthList:
     def __init__(self, maxsize=0): = []
         self.maxsize = maxsize

     def append(self, item):
         if self.maxsize and len( == self.maxsize:

    def __getattr__(self, attr):
        return getattr(, attr)

I think it would be perfectly reasonable for Python to choose to instantiate
my MaxLengthList instead of a plain old list.

    Yves> I am lazy for declaring it first, IMHO it bloats the code, and
    Yves> (don't know if it's good to say that here) where I come from (php)
    Yves> I was used to not-needing it...

I don't know php, but would also have to wonder how it knows to create a
list instead of an integer (for example).

Finally, from the Zen of Python (try "import this" at an interpreter

    In the face of ambiguity, refuse the temptation to guess.


More information about the Python-list mailing list