Python datatypes
Python boasts a rich set of built-in datatypes that you can use without importing any modules. Most of these, with the exception of the set that we introduce shortly, have syntax for including them within your code. To understand the examples in this book or to write Python yourself, you're going to need to recognize them. Table 2.1 contains a guide to all the basic datatypes and their syntaxes.
|
Datatype |
Type |
Syntax examples |
Notes |
|
Byte string |
str |
'hello world' "also hello world" """A triple quoted multiline string""" '''Another triple quoted multiline string''' |
In IronPython, byte strings and Unicode strings are the same datatype. |
|
Unicode string |
u"Another Unicode string" u"""Yet another Unicode string.""" | ||
|
Integer |
int |
-3 | |
|
Long integer |
long |
999999999999999999999 9L | |
|
Floating point |
float |
0.0 -3.1 2e100 2.765e-10 | |
|
Complex numbers |
complex |
2 + 3j 8j |
|
Datatype |
Type |
Syntax examples |
Notes |
|
List |
list |
[1, 2, 3, 4] [1, "a", 3.0] |
An empty list. A populated list. A list containing members of different types. |
|
Tuple |
(1, 2, 3, 4) 1, 2, 3, 4 |
An empty tuple. Tuple with a single member.8 Although tuples are usually created with parentheses, they're only needed to disambiguate in situations where commas have other significance (such as argument lists in function calls). It's the commas that are significant in creating a tuple. | |
|
Dictionary |
diet |
{'key': 'value', 'key2': 'value2'} |
An empty dictionary. A populated dictionary. |
|
set([1, 2, 3, 4]) |
Creates an empty set. Creates a set from a list. | ||
|
None |
NoneType |
None |
Equivalent of NULL in other languages. |
|
Boolean |
bool |
True False |
a. That trailing comma is important! In the case of a tuple with only one member, It's needed to disambiguate a tuple from an ordinary expression surrounded by parentheses.
a. That trailing comma is important! In the case of a tuple with only one member, It's needed to disambiguate a tuple from an ordinary expression surrounded by parentheses.
This table isn't exhaustive. For example, it doesn't include things like the slightly exotic frozenset or the built-in exceptions.
In addition to the information in the table, you need to know some additional things about these datatypes. STRINGS
You include Python string literals in your code with quotes. You can use single quotes or double quotes—there's no difference. If you want to include literals with newline characters, you can use triple-quoted strings.
You can also use normal Unix-type escape characters.5 For example, here's a string with a hard tab separating the two words and terminated with a newline character:
'hello\tworld\n'
5 See this page for a full list of the escape characters: http://docs.python.org/ref/strings.html.
The standard string type is often referred to as the byte string; the contents are stored internally as a sequence of bytes representing the characters. Byte strings can also be used for storing binary data.
Python has a second string type, the Unicode string. You create a Unicode string literal by prefixing your string with a u.
u'This is a Unicode string'
NOTE Because IronPython is built on top of .NET, the Unicode and string types are the same type. This is better than having two string types (and is the position that Python will be in by Python 3.0), and you should have less encoding-related problems with IronPython than you would with CPython.
Both str and unicode strings have a common base class: basestring. If you want to check if an object is a string and you don't care which type of string, you can use isinstance(someObject, basestring). This isn't important for IronPython, but can be useful for compatibility with CPython.
You may stumble across a few other syntax permutations for strings. For example, by prefixing a string with an r, you make it a raw string. Backslashes aren't interpreted as escape characters in raw strings.6 Raw strings are especially useful for regular expressions where backslashes have syntactic meaning and having to continually escape them makes regular expressions less readable.
All the built-in types (except for None and tuples) have a large number of methods that do useful things. Strings are no exception; they have methods for trimming whitespace, joining strings, checking if they contain substrings, and much more. For a list of the Python methods available on strings, visit http://docs.python.org/lib/ string-methods.html.
If you're a .NET developer, then you'll already be familiar with the .NET methods available on strings. THE CLR MODULE AND IRONPYTHON TYPES
One thing that makes working with IronPython so straightforward is that the basic datatypes are both Python objects and .NET objects. An IronPython string is also a .NET string—which is clever magic. By default the .NET methods aren't available on the basic types such as strings.
Traceback (most recent call last):
AttributeError: 'str' object has no attribute 'ToUpper'
The IronPython developers made this decision after much debate; in the end, they felt that leaving extra attributes on Python objects wouldn't be a faithful implementation of Python. You can enable the .NET methods by importing the clr module.
6 With the odd exception that a raw string can't end in an odd number of backslashes; otherwise, the last backslash escapes the closing quote.
'HELLO WORLD'
NOTE The clr module provides functions for interacting with the underlying .NET runtime. It's the basic entry point for .NET/IronPython interoperation, and we use it a great deal throughout the book.
You can easily determine that IronPython strings are also .NET strings. If you compare the Python str type with the .NET System.String type, you'll see that they are, in fact, the same object.
True
When using the basic types, you can choose whether to use .NET patterns or Python patterns of programming from within IronPython code. As we go through the book, you'll find that we also have this choice when dealing with higher level concepts such as working with files or threading.
We've looked at strings; now let's look at the next most common objects—numbers. NUMBERS
Python has four types for representing numbers: integers, long integers, floating point numbers, and complex numbers.
Integers are for representing whole numbers, positive or negative, up to sys. maxint,7 which is a value that depends on the underlying platform. Long integers are used to represent integers that are greater than this number. Internally, Python promotes large numbers into longs automatically; in practice, you'll rarely care whether a number is an int or a long. With Python long integers, the maximum number you can represent is only limited by the amount of memory you have. That's likely to be quite a big number.
You can mix operations involving integers and floats; the result will always be a float. If you divide two integers, you'll get an integer back. This can be surprising if you aren't expecting it. If you need to get the real value of a division back, then make sure that one of the values is a float.
Python also has syntax for complex numbers, which are unusual, but handy for certain kinds of math. Having said that, I don't think I've ever needed to use them.
7 Typically on a 32-bit operating system, this will be the largest number that can be stored in a 32-bit signed integer: 2147483647.
Post a comment