On 6/6/07, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote: > In <[EMAIL PROTECTED]>, Francesco > Guerrieri wrote: > > > Now the question is this: > > I would like to initialize such an object in this way: > > a = myList() > > a = [[1, 2, 3], [4, 5, 6, 7]] > > a.pad() > > # and now a _should_ contain [[1, 2, 3, ""], [4, 5, 6, 7]] > > > > > > Obviously this doesn't work, because when at the second line I do the > > initialization, type(a) becomes <type 'list'>, and so I get the > > expected AttributeError since pad cannot be found. > > You don't initialize in the second line, you just rebind `a` to a > completely different object. Names don't have types in Python, objects do. > > `list()` takes an optional argument. Just make sure your derived type > does to and passes this to the base class `__init__()`. Then you can > create an instance like this: > > a = MyList([[1, 2, 3], [4, 5, 6, 7]]) >
yes it's true that it is not an initialization :-) It's that I hoped that there was a way to do an init rather than a rebinding of the name. Your suggestion is exactly what I have implemented for the time being... I subclass the builtin list type, I have a pad method which adds the requested whitespaces at the end, an append method which invokes the base class append AND calls the pad method, and finally a __call__ which calls the append. Furthermore, I check that the input is valid... So everything works fine :-) The only problem is that while coding I have the temptation to write a = [[...], [...]) rather than a([1, 2, 3], [5,6, 7, 8]). Plus I find it uglier :-) but if there isn't a reasonable way, I'll give up :-) thanks, Francesco -- http://mail.python.org/mailman/listinfo/python-list