< BACKMake Note | BookmarkCONTINUE >
152015024128143245168232148039199167010047123209178152124239215162147039203228119224069233

An Introduction to Python OOP

A class defines a category of objects in terms of the data it encapsulates and the operations on the data that are allowed by the interface functions. Essentially, a class is a template from which objects can be created.

Each object created from a class is an instance of a class. They all look alike and exhibit a similar behavior.

A class stores object attributes (also known as data members) and the behavior of objects (mostly known as methods). This behavior can be inherited from other (base) classes. The non-method attributes of the class are usually referred to as class members or class attributes so that they are not confused with instance attributes.

Each class has its own namespace in which all the assignments and function definitions occur.

Class Instances

A class instance is a Python object, and similar to every Python object, it has the following properties: identity, object type, attributes, methods, and value.

I will use the following class definition as the basis for the next explanations. First, let's declare the c class, and then we will create an instance of this class called obj.

						
>>> class c:
…     def __init__(self, value=None):
…         self.name = value
…
>>> obj = c()
>>> obj.name = "Andre"

					

The identity is the memory location allocated for the object. It can be identified by using the id() function.

						
>>> id(obj)
6623988

					

The object type is the object's internal representation. It defines the supported methods and operation for each object. You can use the type() function in order to find out the type of a specific object.

						
>>> type(obj)
<type 'ínstance'>

>>> type(obj.name)
<type 'string'>

					

While we're talking about object types, let's take a quick break from the whole class issue and examine the types for Python objects defined in extension modules, which do not necessarily act like classes.

Table 5.1 lists all Python built-in object types defined by the types module. Note that almost all the types shown in this table are unrelated to Python classes.

Table 5.1. Built-In Object Types Defined by the types Module
Built-In Object Type Description
NoneType the None (null) object
IntType integer
LongType arbitrary precision integer
FloatType floating point
ComplexType complex number
StringType list of characters
ListType list
TupleType tuple
XrangeType returned by xrange()
DictType dictionary
BuiltinFunctionType built-in functions
BuiltinMethodType built-in methods
FuntionType user-defined function
ClassType class object/definition
InstanceType class object instance/class instance
MethodType bound class method
UnboundMethodType unbound class method
ModuleType module
FileType file
CodeType* raw byte-compiled code
FrameType* represent execution frame
TracebackType* stacks the traceback information of an exception
SliceType* generated by extended slices
EllipsisType* it is used in extended slices
*The checked types indicate internal Python objects that can be exposed to the user.

The attributes and methods of an object are bound properties that must be accessed by putting a dot (.) after the object name.

						
>>> obj.name
"Andre"

					

At last, the value of an object is better visualized by an example.

						
>>> obj.name = "Andre"

					

The string "Andre" is the value assigned to the name attribute of the object obj.


Last updated on 1/30/2002
Python Developer's Handbook, © 2002 Sams Publishing

< BACKMake Note | BookmarkCONTINUE >

Index terms contained in this section

attributes
      classes
      instance
attributes property
base classes
BuiltinFunctionType object type
BuiltinMethodType object type
class attributes
class instances 2nd 3rd
class members
classes
      base
ClassType object type
CodeType object type
ComplexType object type
DictType object type
EllipsisType object type
FileType object type
FloatType object type
FrameType object type
FunctionType object type
identity property
instance attributes
instances
      classes 2nd 3rd
InstanceType object type
IntType object type
ListType object type 2nd
LongType object type
members
      class
methods property
MethodType object type
modules
     types
            built-in object types
ModuleType object type
NoneType object type
object type property 2nd
object types
      types module
object-oriented programming (OOP) 2nd 3rd
programming
      object-oriented (OOP) 2nd 3rd
properties
      attributes
      identity
      methods
      object type 2nd
      value
SliceType object type
StringType object type
TracebackType object type
TupleType object type
types module
      built-in object types
UnboundMethodType object type
value property
XrangeType object type

© 2002, O'Reilly & Associates, Inc.