You can define new types, or
classes, with Python's
class declaration. Some definitions:
A type (built-in or user-defined).
One value of a given type. As the number
23 is an object of type
int, so an object in general
is one instance of a user-defined type. It is useful
to think of a
class a cookie cutter (that is, a pattern), and an object
as a cookie that follows that pattern. Like individual
cookies, individual objects all start out looking the
same, but inside them are values that may change.
The type or class defines the behaviors common to all
objects of the same type. For example, the unary
- operator works on all
values of type float, and
changes the sign of the value.
Same as “object.”
One of the values inside an instance. For
example, a complex number C
has two float values inside
it: the real pattern is referred to as
C.real and the imaginary part
as C.imag. Ultimately all
the values inside an instance are the atomic types
listed in the table of
basic types, or other objects that themselves
contain atomic types, and so on.
A function that operates primarily on an object.
Unlike regular Python
functions, which are called with the function
name (such as “sqrt(x)”), to
call a method you use the syntax:
O.m(...)
where is an object,
O is the method name, and
you supply zero or more arguments between
parentheses.m
To define a new class of objects:
classC:Bidefn0( self, ... ):B0defn1( self, ... ):B1...
This construct defines a new class
.C
Block
is executed
once when the class is first scanned, and can be used to define
class variables and methods.Bi
The functions
define the methods of the class. Note that these
nidef statements must be indented
below the class
declaration.
When you are defining methods, you must always
include self as the first
argument. When you call a method, you omit that first
argument. For example, if a method is defined as:
class Antenna:
def rotate(self, degrees):
...then, if you had an instance of this class named
hill, you might call the method as
hill.rotate(40.3)
Inside a method, the name
self is used to refer to the
class's members. For example, inside the
.rotate() method, you might refer
to member height as
self.height.
To define a derived class that inherits from
superclasses
,
P0, ...:P1
classC(P0,P1,...):
You can inherit from built-in classes such as
int and
dict. See below under
the class constructor for more details.
You can use a number of special method names to
define certain behaviors of your objects. All these
names start and end with two underbars
(__). The most commonly used one
is __init__(), the class
constructor, but there are many more to define how your
objects act when operated on by various operators.
The constructor is a method
that is executed to create a new object of the class.
Its name is always __init__().
Its parameter list must always include
self as the first argument; any
remaining parameters must be provided by the caller.
For example, if your constructor has four parameters
including self, a caller must
provide three arguments.
You can think of self as
representing the name space inside the instance. To
add new members to an instance, just assign a value to
self.
where v is the member name.v
Here's a complete example of a class
FeetInches that
represents dimensions in feet and inches. Internally
the dimension is represented as only inches, and that
value is stored in a member named
.__inches (member names starting
with two underbars, like this, one, are
private members, and hence
accessible only from inside the object).
The constructor takes two arguments named
feet and
inches and converts them to the
private member .__inches. Then
we define a method called
.show() that converts the
internal dimension to a string of the form
where
f ft
i in
is the whole feet and
f is
the inches part. Here's the class definition:i
class FeetInches:
def __init__ ( self, feet, inches ):
self.__inches = (feet * 12.0) + inches
def show(self):
feet = int ( self.__inches / 12.0 )
inches = self.__inches - feet * 12.0
return "%d ft %.2f in" % (feet, inches)To create an object of this type we might say:
fiveFootFour = FeetInches(5, 4)
The method call
fiveFootFour.show() would then
yield the string "5 ft 4.00 in".