overriding setting

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Wed Jun 6 06:34:38 EDT 2007


In <mailman.8748.1181124863.32031.python-list at python.org>, 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]])

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list