[Tutor] Re: How to subclass built-in types?

Derrick 'dman' Hudson dman@dman.ddts.net
Sun, 29 Sep 2002 13:15:59 -0400


--r5Pyd7+fXNt84Ff3
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Sun, Sep 29, 2002 at 06:40:51PM +0200, Gregor Lingl wrote:

| Next example:
|=20
| >>> class M(list):
|    def __init__(self, x):
|        self.x =3D x
|
| >>> e =3D M(3)
| >>> e
| []
| >>> e.x
| 3
|=20
| This constructor only constructs empty lists.
| Now, how do I define a constructor, which, for instance as
| the first argument takes a sequenze to initialize itself (as
| a list) and more arguments for other purposes.
|=20
| This class should behave like the following:
|=20
| >>> class N(list):
|    def __init__(self, seq, x):
|        self.x =3D x
         list.__init__( self , seq )
        #^^^^ superclass object

| Who knows how to do it "according to the rules"?

It's the same as for non-builtin types.  (note: python >=3D 2.2 must be
used to subclass built-in types)

-D

--=20
If your life is a hard drive,
Christ can be your backup.
=20
http://dman.ddts.net/~dman/

--r5Pyd7+fXNt84Ff3
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iEYEARECAAYFAj2XNU8ACgkQO8l8XBKTpRS1iwCfX7atGDUyWkiOdlqME7lvJ5M4
Gc8AniRUecrAJAFhaorAdvil22PNDQKB
=Ei7h
-----END PGP SIGNATURE-----

--r5Pyd7+fXNt84Ff3--