Functions Methods and Operators Terminology

The term function is used to refer to a subroutine that can be executed independently, and the term method is used to refer to a function that can only be executed when bound to an object, that is, called on an instance of a particular class.

An operator may be independent or it may be bound to an object, but unlike functions and methods, operators do not use parentheses. Operators that are represented by symbols such as +, *, and < are rather obviously called operators, but operators that have names such as del and print are often called statements.

Python functions do not have to be pure in the mathematical sense: They do not have to return a value and they can modify their arguments. Python functions are like C and C++ functions, or like Pascal functions that take var parameters. Python methods are like C++ or Java member functions.

Python has two ways of comparing objects: by "identity" and by "value". An object's identity is effectively its address in memory, and this is what an object reference holds. If we use comparison operators, such as == and <, we get value comparison. For example, two strings are equal using == if they both contain the same text. If we use is we get identity comparison, which is fast because we are just comparing two addresses and don't have to look at the objects themselves. An object's identity can be obtained by calling id() on an object reference.

Python has a special object called None. This can be assigned to any variable and it means that the variable has no value. There is only ever one instance of the None object, so we can always use the fast is and is not comparisons when testing for it.

Notice that we wrote x on its own at the >>> prompt. If we write an expression or variable in IDLE, its value is automatically printed. In a program, we must use an explicit print statement to print an expression. For example:

print x

Python's print statement is an operator, not a function, and for this reason it is invoked without using parentheses (just as we use + and other operators without them).

Earlier we said that Python uses dynamic typing. There are two factors involved in this. First, we can assign any object to any variable; for example, we could write:

After the first line x's type is int, and after the second line x's type is str, so clearly the type associated with the name x is determined by what the name is bound to, and not by any intrinsic property of its own. For this reason , we do not need to associate a particular type with a particular name.

The second aspect of Python's dynamic typing is that the typing is strong: Python does not permit operations between incompatible types, as the following example, typed into IDLE, shows:

Traceback (most recent call last): File <pyshell#2>, line 1, in <module> x + y

TypeError: unsupported operand type(s) for +: 'int' and 'str'

When we attempted to apply the binary + operator, Python raised a TypeError exception and refused to perform the operation.* (Exceptions are covered in Chapter 2.)

If we were to assign to y a type compatible with x's type, such as an int or float, the addition would work fine:

>>> x = 41 >>> y = 8.5 >>> x + y 49.5

Although x and y are of different types (int and float), Python provides the same kind of automatic type promotion that other languages use, so the x is converted to a float and the calculation performed is actually 41.0 + 8.5.

Figure 1.2 Object references and objects

Assigning a value to a variable is called binding, since we bind names to objects. If we assign a new object to an existing variable, we are said to be

The rectangles represent the objects, and the circles the object references, that result from the execution of the code shown.

★ The line of the traceback, File "<pyshell#2>", and so on, varies every time, so your line may be different from the one shown here.

rebinding the name. This is illustrated in Figure 1.2. When we do this, what happens to the object the name was originally bound to? For example:

>>> x = "Sparrow" >>> x = 9.8

What has happened to the str object that holds the text "Sparrow"? Once an object has no names bound to it, it is scheduled for garbage collection, and in due course it may be deleted from memory. This is very similar to how things work in Java.

Python variable names consist of ASCII letters, digits, and underscores (_). Variable names should begin with a letter, and they are case-sensitive (rowan, Rowan, and roWan are three different variables). No Python variable should be given the name of any of Python's keywords (see Table 1.1), nor of Python's built-in constants such as None, True, or False.

Was this article helpful?

0 0
Tuberminator

Tuberminator

The main focus of this report is to show how to get involved in video marketing on the run, how to rank quickly on YouTube and Google using FREE semi-automatic tools and services. QUICKLY AND FREE. I will show methods and techniques I use to rank my videos, as well as free resources and tools to make video clips, to get backlinks and free traffic.

Get My Free Ebook


Post a comment