The definition of a class specification is begun using the class keyword, followed by a
The implementation code, which defines how the class is constructed and operates, is
indented relative to the start of the class keyword, in keeping with the style of other
separated code blocks elsewhere in Python. In particular, functions are usually defined
within the indented code block of a class, such that these functions ‘belong’ to that class
definition.
For defining a subclass, which is based on the definition of some other class, we have to
Unlike many computer languages (e.g. Java), Python allows multiple inheritance,
which is to say that a class can have more than one superclass, and inherit properties from
all of these. In this case the names of the superclasses are listed, separated by commas,
within the parentheses. Multiple inheritance, however, is generally rare; it is much more
common to have only one superclass, like the Protein above, or no superclass at all like
Molecule.
In Python 2 there is a slight complication with Python classes, which stems from the
fact that ‘new-style’ classes were introduced, for technical reasons.
version of Python from 2.2 to 2.7 you can use the new style by making the class definition
a subclass of the basal Python class, which uses the keyword object.
2
class Molecule(object): # New style
# contents of the implementation
In Python 3 all classes inherit from object and you do not have to specify this explicitly
(although you can).
When writing your own class code, it is common practice, but certainly not mandatory,
to save each class definition within a single, dedicated file on disk and to call that file after
the name of the class. For example, we might have a class called Molecule implemented in
a file named ‘Molecule.py’ and Protein implemented in one named ‘Protein.py’. Generally
this goes along with the notion that your class definitions may be useful in different
situations, whereupon each definition can be imported, to be used within any Python
program as needed. To access such classes, saved in other files (modules), one has to
import the class, for example, here via:
from Molecule import Molecule
and
from Protein import Protein
Above, the first name in each line is the name of the module, which relates to the name
of the file (‘Molecule.py’ or ‘Protein.py’) from which to load data. Note that the ‘.py’
suffix of the file name is not included. The second name, after the import statement, is the
name of the class (Molecule, Protein). It is only our convention that the module names and
class names are the same, but it is a common one which you should be familiar with.
There is an alternative approach, especially for closely related classes, where multiple
class definitions are contained in a single file. For example, we could decide to put both
Molecule and Protein into the same file, here somewhat arbitrarily called ‘Molecules.py’.
In this case the import, assuming you need access to both classes in your code, would
instead look like:
from Molecules import Molecule, Protein
How exactly object class definitions are arranged, in terms of which module they reside
in and what the module names are, are both matters of taste and convenience.
Do'stlaringiz bilan baham: