append to non-existing list
skip at pobox.com
skip at pobox.com
Wed Nov 9 08:15:31 EST 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.data = []
self.maxsize = maxsize
def append(self, item):
if self.maxsize and len(self.data) == self.maxsize:
del self.data[0]
self.data.append(item)
def __getattr__(self, attr):
return getattr(self.data, 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