Class information
Normally when an object is manipulated it is just the attributes and functions associated
with it that are of interest. However, sometimes the class itself is of interest. There is a
special object attribute called __class__
7
that gives a handle on the class definition of an
object, although it should be noted that this exists only for user-defined classes:
molecule.__class__ # same as: Molecule
It might not seem obvious why you would need this, given that the class definition was
required to make the object in the first place. Nevertheless, it is surprisingly useful in more
complex situations where you are manipulating objects from a number of different classes,
and you need to check that the right thing happens to the right kind of object.
The class definition itself has a special attribute called __name__ that gives the class
name as a string:
Molecule.__name__ # 'Molecule'
These two special attributes do not appear in the internal attribute dictionary __dict__.
Classes have another special attribute called __doc__, which does appear in the
__dict__. This is used for adding textual comments to help document the class, in the
same way that the corresponding attribute (also named __doc__) is also used to document
a function. It is filled in from the triple-quoted Python string that appears immediately
after the class definition, if such a string exists:
class Molecule:
'''This class describes a biological molecule'''
# …
Molecule.__doc__ # 'This class describes a biological molecule'
As well as attributes accessed from objects and classes, there are also inbuilt Python
functions that operate on classes and objects to say something about them. There is a
function, isinstance(), that checks whether a specified object is an instance of a class (was
made with that definition). The class might not be the direct class of the object but could
also be a superclass:
molecule = Molecule('moleculeName')
isinstance(molecule, Molecule) # True
isinstance(molecule, Protein) # False
molecule = Protein('proteinName', 'QWERTY')
isinstance(molecule, Molecule) # True
isinstance(molecule, Protein) # True
This can be useful when you have objects that derive from a known class, but which
include a mixture of subclass and superclass versions, where you only want to operate in a
given way if it is actually a member of the subclass, with its extra bits:
if isinstance(molecule, Protein):
aminoAcids = molecule.aminoAcids # exists since it's Protein
# …
There is an associated function, issubclass(), that works on two classes instead of an
object and a class:
issubclass(Molecule, Molecule) # True
issubclass(Molecule, Protein) # False
issubclass(Protein, Molecule) # True
issubclass(Protein, Protein) # True
Related to detecting which kind of object you have, there is another concept called the
type of an object. The type of an object is given by the inbuilt type() function. In Python 3
the type of an object gives its class, and the same is true in Python 2 for ‘new-style’
classes, which inherit from object. In Python 2 all user-defined ‘old-style’ classes have the
same type.
type(3) # in v3, in v2
type(3.14) # in v3, in v2
type('red') # in v3, in v2
type(()) # in v3, in v2
type([]) # in v3, in v2
type(set()) # in v3, in v2
type({}) # in v3, in v2
type(molecule) # in v3
# and in v2, new style class
# in v2, old style class
The type() function actually gives you back another Python object, and the type of such
an object is the seemingly odd in Python 3, or in Python 2.
The type objects can also be obtained otherwise:
int # in v3, in v2
float # in v3, in v2
str # in v3, in v2
tuple # in v3, in v2
list # in v3, in v2
set # in v3, in v2
dict # in v3, in v2
Molecule # in v3 and v2
In Python 2 for ‘old style’ classes, to get at the type of Molecule without using the
type() function we need to import the types module and then we have:
import types
types.InstanceType # in v2 old style class
Checking the object type comes in handy when some variable could have one of a few
types, and how it gets processed depends on the type. For example, a function might have
an argument that could be a tuple or a dictionary, where the following code could be used:
def f(x):
if isinstance(x, tuple):
# do something
elif isinstance(x, dict):
# do something else
else:
# error: raise an exception
Do'stlaringiz bilan baham: |