2.2 Why Use Built-in Types?

2.2 Why Use Built-in Types?

If you've used lower-level languages such as C or C++, you know that much of your work centers on implementing objects ”what some folks call data structures ”to represent the components in your application's domain. You need to lay out memory structures, manage memory allocation, implement search and access routines, and so on. These chores are about as tedious (and error prone) as they sound, and usually distract from your programs' real goals.

In typical Python programs, most of this grunt work goes away. Because Python provides powerful object types as an intrinsic part of the language, there's no need to code object implementations before you start solving problems. In fact, unless you have a need for special processing that built-in types don't provide, you're almost always better off using a built-in object instead of implementing your own. Here are some reasons why:

Built-in objects make simple programs easy to write

For simple tasks , built-in types are often all you need to represent the structure of problem domains. Because we get things such as collections (lists) and search tables (dictionaries) for free, you can use them immediately. You can get a lot of work done with just Python's built-in object types alone.

Python provides objects and supports extensions

In some ways, Python borrows both from languages that rely on built-in tools (e.g., LISP), and languages that rely on the programmer to provide tool implementations or frameworks of their own (e.g., C++). Although you can implement unique object types in Python, you don't need to do so just to get started. Moreover, because Python's built-ins are standard, they're always the same; frameworks tend to differ from site to site.

Built-in objects are components of extensions

For more complex tasks you still may need to provide your own objects, using Python statements and C language interfaces. But as we'll see in later chapters, objects implemented manually are often built on top of built-in types such as lists and dictionaries. For instance, a stack data structure may be implemented as a class that manages a built-in list.

Built-in objects are often more efficient than custom data structures

Python's built-in types employ already optimized data structure algorithms that are implemented in C for speed. Although you can write similar object types on your own, you'll usually be hard-pressed to get the level of performance built-in object types provide.

In other words, not only do built-in object types make programming easier, they're also more powerful and efficient than most of what can be created from scratch. Regardless of whether you implement new object types or not, built-in objects form the core of every Python program.

Table 2.1 previews the built-in object types in this chapter. Some will probably seem familiar if you've used other languages (e.g., numbers , strings, and files), but others are more general and powerful than what you may be accustomed to. For instance, you'll find that lists and dictionaries obviate most of the work you do to support collections and searching in lower-level languages.

Table  2.1. Built-in Objects Preview

Object Type

Example Constants/Usage

Numbers

 3.1415, 1234, 999L, 3+4j 

Strings

 'spam', "guido's" 

Lists

 [1, [2, 'three'], 4] 

Dictionaries

 {'food':'spam', 'taste':'yum'} 

Tuples

 (1,'spam', 4, 'U') 

Files

 text = open('eggs', 'r').read() 


Learning Python
Learning Python: Powerful Object-Oriented Programming
ISBN: 0596158068
EAN: 2147483647
Year: 1999
Pages: 156
Authors: Mark Lutz

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net