Getting Started with Python
Getting Started with Python
Written by Alex Armstrong   
Wednesday, 08 February 2012
Article Index
Getting Started with Python
Control structures



In Python a function is a valid example of an object.

To define a function you use the def command, complete with parameters if needed, and a return statement to return a value. For example:

>>> def add(x,y):
 return x+y
>>> add(2,3)

Notice that parameters are typeless and this is both a convenience and a possible problem. For example, with the above definition of add the following works perfectly:

>>> add("lumber","jack")

If you find this strange and worrying then it is perhaps because you have been in the grip of strongly typed languages for too long!

When you write the add function you are thinking about “adding” together two objects and it is up to the system to work out what you mean by “+” for any two objects you care to pass.

Before you ask – yes it works for lists as well:

>>> add([1,2],[3,4])
[1, 2, 3, 4]

One more complication is that we need to know the scope of a function or a variable definition.

The basic idea is that a name belongs to the name space that is appropriate to where it is first used – local, global or built in.

If you want to make a variable name global from within a function you have to declare it as such using “global”.

Arguments are also influenced by the everything is an object  idea and the fact that variable hold references and not values.

In short all parameters are passed by reference.

This results in a behavior which many programmers will regard as strange.

For example suppose you write:

def test(a):

Then when you use this function with a call like:


the value of x is unchanged by the assignment within the function.

Ah, you might say, this is pass by value, but not so.

What is happening is that when the function is called the local variable “a” is set to “point” to or to reference the same object that “x” is “pointing” at.Thus when a is accessed it returns 2.

When the 1 is assigned to “a” the variable it is treated as an object and a is simply set to point or reference the new object i.e. 1. The original variable is still pointing or referencing the original object i.e. “x” is still pointing at the object 2.

When you work with simple object like numbers then this does look like pass by value but as soon as you pass a more complex object it becomes clear that it is pass by reference:

def test(a):
a[0]= "spam"

and use


you will discover that the list in the calling program has its first item changed to “spam”.

The reason for the difference is that you have changed part of the object that both variables are pointing at.

There are also facilities for passing arguments by name, variable numbers of parameters and parameter defaults.

For Lisp enthusiasts, Python has a lambda expression facility, which allows any expression to be treated essentially as a data object.

An Object of My Own

Moving on to more traditional object-oriented facilities brings us to the class statement. Python implements objects using the traditional mechanism of defining a class and then creating instances of the class.

You can define a class complete with variables and functions, i.e. methods and create instances of the class by assignment.

Python objects use the standard "dot" notation for methods. For example, MyObject.MyMethod() calls the function MyMethod defined in MyObject.

This sounds all too easy and I have to admit that it really is easy. The only minor complication is that you have to use the keyword “self” as a reference to the particular instance of the class.

Consider, for example,

class point:
def setpos(self,x,y):
def display(self):
print self.position

This defines a class called point which has two methods – setpos and display. Each of these has the default parameter “self” included. The methods use “self” to qualify any variables that are specific to the instance of the class.

You can now create an instance of the class using:


Notice that the brackets are all-important – without them you create variables pointing to the class, not the instance. This is a big difference between Python and other object-oriented languages - the class is actually an object that is created on an almost equal footing with its instances. You can create class data, for example, by using variables outside of methods.

You can refer to methods using the usual dot a.method() notation.

For example,




Last Updated ( Wednesday, 08 February 2012 )

RSS feed of all content
I Programmer - full contents
Copyright © 2018 All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.