Not a regular printable symbol.
7
Object orientation
Contents
Creating classes
Class definition
Class functions
Class and object attributes
Class constructors
Example: molecule, protein and amino acid classes
Further details
Class and object dictionary
String attribute access
Class information
Creating classes
For simple tasks involving short programs, you can survive perfectly well with the
standard Python data types for holding information, such as lists and dictionaries.
However, for more complicated tasks involving long programs, this often becomes
unwieldy. There are various ways to deal with this issue, but one of the most fruitful is the
ability to define your own data types: objects built to your own specification, organised in
the way that is convenient to you. Modern computer languages do this via the introduction
of bespoke object definitions that are known as classes and this kind of thinking is
generally termed object-oriented programming.
When creating your own custom data types, the class is the definition of a particular
kind of object in terms of its component features and how it is constructed or implemented
in code. The term object, however, refers to a specific instance, or occurrence, of the thing
which has been made according to the class definition. The making of an object of a given
class is what is usually termed instantiation. A convenient analogy is to think of the
blueprint for a house being like a class, but the actual, solid house being the object. Also,
given a single blueprint one may build many instances of different house objects, all to the
same design. It is quite common to use the words ‘class’ and ‘object’ interchangeably,
even in the same context, although they mean different things, and it is important to
understand the difference. As it happens, everything that is brought into existence in
Python is an object, so even integer and floating point numbers are objects, although most
of the time you can work without noticing that.
There are various definitions of what constitutes object-oriented programming, and here
the exact details do not matter much, since we are using what Python has provided in this
regard. One common principle seen in many programming languages is that the class
definition should make available certain useful functionality, and that internal information,
about how a specific class is implemented, should be hidden from the outside. This is
called information hiding and encapsulation. In this regard, however, Python does not take
a particularly strict view and you can prod and probe virtually every part of an object, if
you know how. Nonetheless, you can work as though there were encapsulation, and we
will use this practically in the next chapter. As an example, suppose you have a Molecule
class that models how biological molecules are constructed. This might have defined a
function called getName(), which gives back the name of the molecule as a text string. For
the person who is using this function, there should be no requirement to know how the
molecule’s name is stored internally or how the function is implemented, just that the
function exists and provides the name of the molecule.
Do'stlaringiz bilan baham: