append to non-existing list

Yves Glodt y.glodt at
Wed Nov 9 14:32:07 CET 2005

skip at wrote:
>     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?

I am fairly new to python (and I like it more and more), but I can not 
answer this question... As I said, where I come from it is possible, and 
how they do it is explained a little here:

As I want to learn, I will work with python the pythonic way, but 
sometimes I just fall back to what I know from php... :-)

Thanks for your answer and the example code,

thanks for the "import this" hint

> 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:
>              del[0]
>     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
> prompt): 
>     In the face of ambiguity, refuse the temptation to guess.
> Skip
> .

More information about the Python-list mailing list