Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

Data + Processing = Objects

Combining Data and Processing into Class Definitions

In Part I, “Language Basics”, we examined the core statements in the Python language. In Part II, “Data Structures”, we examined the built-in data structures available to us as programmers. Using these data structures gave us some hands-on experience with a number of classes. After using this variety of built-in objects, we are better prepared to design our own objects.

Chapter 21, Classes introduces basics of class definitions and Chapter 22, Advanced Class Definition introduces simple inheritance. We extend this discussion further to include several common design patterns that use polymorphism. In Chapter 23, Some Design Patterns we cover a few common design patterns. Chapter 24, Creating or Extending Data Types describes the mechanism for adding types to Python that behave like the built-in types.

We will spend some additional time on Python's flexible notion of "attribute" in Chapter 25, Properties and Descriptors . It turns out that an attribute can be a simple instance variable or it can be a method function that manages an instance variable.

We'll look at Python's decorators in Chapter 26, Decorators ; this is handy syntax for assuring that specific aspects of a family of classes are implemented consistently. We'll look at the various ways to define properties in objects.properties. Additionally, we'll look how we can manage more sophisticated object protocols in Chapter 27, Managing Contexts: the with Statement .

Data Types. We've looked at most of these data types in Part II, “Data Structures”. This is a kind of road-map of some of the most important built-in features.

  • None. A unique constant, handy as a placeholder when no other value is appropriate. A number of built-in functions return values of None to indicate that no useful work can be done.

  • NotImplemented. A unique constant, returned by special methods to indicate that the method is not implemented. See the section called “Special Method Names” for more information.

  • Numeric types have relatively simple values.

    • Boolean. A variety of values are treated as logically false: False, 0, None, "", (), [], {}. All other values are logically True.

    • Integer. Typically 32-bit numbers with a range of -2,147,483,648 through 2,147,483,647. On some platforms, these may be 64-bit numbers.

    • Long. These are specially coded integers of arbitrary length. They grow as needed to accurately represent numeric results.

    • Float. These are floating point, scientific notation numbers. They are represented using the platform's floating point notation, so ranges and precisions vary. Typically these are called "double precision" in other languages, and are often 64-bits long.

    • Complex. These are a pair of floating point numbers of the form ( a + b j), where a is the real part and b is the “imaginary” part.

  • Sequence. Collections of objects identified by their order or position.

    • Immutable sequences are created as needed and can be used but never changed.

      • String. A string is a sequence of individual ASCII characters.

      • Unicode. A Unicode string is a sequence of individual Unicode characters.

      • Tuple. A tuple is a simple comma-separated sequence of Python items in ()'s.

    • Mutable sequences can be created, appended-to, changed, and have elements deleted

      • List. A list is a comma-separated sequence of Python items in []'s. Operations like append and pop can be used to change lists.

  • Set and Frozenset. Collections of objects. The collection is neither ordered nor keyed. Each item stands for itself. A set is mutable; we can append-to, change and delete elements from a set. A frozenset is immutable.

  • Mapping. Collections of objects identified by keys instead of order.

    • Dictionary. A dictionary is a collection of objects which are indexed by other objects. It is like a sequence of key:value pairs, where keys can be found efficiently. Any Python object can be used as the value. Keys have a small restriction: mutable lists and other mappings cannot be used as keys. It is created with a comma-separated list of key:value pairs in {}'s.

  • Callable. When we create a function with the def statement, we create a callable object. We can also define our own classes with a special method of __call__, to make a callable object that behaves like a function.

  • File. Python supports several operations on files, most notably reading, writing and closing. Python also provides numerous modules for interacting with the operating system's management of files.

There are numerous additional data structures that are part of Python's implementation internals; they are beyond the scope of this book.

One of the most powerful and useful features of Python, is its ability to define new classes. The next chapters will introduce the class and the basics of object-oriented programming.

Table of Contents

21. Classes
Semantics
Class Definition: the class Statement
Creating and Using Objects
Special Method Names
Some Examples
Object Collaboration
Class Definition Exercises
Stock Valuation
Dive Logging and Surface Air Consumption Rate
Multi-Dice
Rational Numbers
Playing Cards and Decks
Blackjack Hands
Poker Hands
22. Advanced Class Definition
Inheritance
Polymorphism
Built-in Functions
Collaborating with max, min and sort
Initializer Techniques
Class Variables
Static Methods and Class Method
Design Approaches
Advanced Class Definition Exercises
Sample Class with Statistical Methods
Shuffling Method for the Deck class
Encapsulation
Class Responsibilities
Style Notes
23. Some Design Patterns
Factory Method
State
Strategy
Design Pattern Exercises
Alternate Counting Strategy
Six Reds
Roulette Wheel Alternatives
Shuffling Alternatives
Shuffling Quality
24. Creating or Extending Data Types
Semantics of Special Methods
Basic Special Methods
Special Attribute Names
Numeric Type Special Methods
Container Special Methods
Iterator Special Method Names
Attribute Handling Special Method Names
Extending Built-In Classes
Special Method Name Exercises
Geometric Points
Rational Numbers
Currency and the Cash Drawer
Sequences with Statistical Methods
Chessboard Locations
Relative Positions on a Chess Board
25. Properties and Descriptors
Semantics of Attributes
Descriptors
Properties
Attribute Access Exercises
26. Decorators
Semantics of Decorators
Built-in Decorators
Defining Decorators
Defining Complex Decorators
Decorator Exercises
27. Managing Contexts: the with Statement
Semantics of a Context
Using a Context
Defining a Context Manager Class

 
 
  Published under the terms of the Open Publication License Design by Interspire