[Tutor] Re: Types of instances

Derrick 'dman' Hudson dman@dman.ddts.net
Thu, 20 Jun 2002 18:16:20 -0500


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


(this message is quick, short, and terse)

On Thu, Jun 20, 2002 at 11:04:58AM +0000, Einar Th. Einarsson wrote:
[determine the class of an object]

class Foo : pass
obj =3D Foo()
print obj.__class__

This is the general way to see the actual class of an object.  To find
out of an object is-a instance of a certain class (or subclass of it
-- subclassing maintains the is-a relationship) use

class SubFoo( Foo ) : pass
o2 =3D SubFoo()
print isinstane( o2 , Foo )


You brought up exception handling.  For that, use the language as it
is intended.  Example :

try :
    some_operation()
except AttributeError :
    print "handling for attribute errors"
except TypeError :
    print "handling for type errors"
except Exception :
    print "handling for other exceptions"


Don't have a single except block that uses isinstance() to branch.
Use the built-in exception handling mechanism to do the branching for
you.  It will be easier to maintain and easier for others to read the
code.

(if this is too terse, come back with more questions)

HTH,
-D

--=20

 "Piracy is not a technological issue. It's a behavior issue."  =20
                                                       --Steve Jobs
=20
http://dman.ddts.net/~dman/


--PNTmBPCT7hxwcZjr
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

iEYEARECAAYFAj0SYkQACgkQO8l8XBKTpRQp+gCcCC/g9tP8afeG1P78FO/GVeM+
uQoAnAsogPIJR7HrMxxI2zY8gsSNlFNM
=epuJ
-----END PGP SIGNATURE-----

--PNTmBPCT7hxwcZjr--