IronPython
Thursday, 15 July 2010
Article Index
IronPython
Significant whitespace
Objects, classes and self
.NET interop
WPF and XAML

Objects

Moving on to more traditional object-oriented facilities brings us to the class statement.

You can define a class complete with variables and functions and create instances of the class by assignment. 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):
self.position=(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.

If you now create an instance of the class using:

a=point()

you can refer to methods using the usual dot a.method()  notation. For example,

a.setpos(1,2)

and

a.display()

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. This is very similar to the way that Javascript works.

If you use a class definition of the form:

class name(superclass):

then the new class inherits all of the methods defined in superclass. For example:

class complexpoint(point)

defines a new class called complexpoint and inherits all of the methods defined in point.

You can, of course, define new methods and override existing methods.

If a method cannot be found in the class’s definition the superclass definition is searched. If the method cannot be found in the superclass then any super-superclass that might exist is searched and so on until a definition is found or an error is generated. You can also arrange to call the inherited method directly using the class name as the first parameter of the call. In fact all method calls are translated from:

instance.method(parameters)

to

class.method(instance,parameters)

and the first parameter is “self”. You can call instance methods directly using this form. For example:

point.setpos(x,1,2)

is entirely equivalent to:

x.setpos(1,2)

Finally it is worth knowing that operator overloading is supported using a range of standard method names.

For example, if you want to define an addition for points then you need the following method:

def __add__(self,other):
self.ans=(self.pos[0]+other.pos[0],
self.pos[1]+other.pos[1])
return self.ans

Now you can write:

z=x+y

where x and y are point objects. Notice that z isn’t a point object but a tuple a special sort of list. If you want to return a point object you have to use:

ans=point()
ans.setpos(self.pos[0]+other.pos[0],
self.pos[1]+other.pos[1])
return ans

Overriding operators might not seem like something you want to do but after using Python for a while I can assure you that you will – if only to write constructors.

The __init__ operator is the objects constructor and if you overwrite it you can define a non-default constructor. For example, if you include:

def __init(self,x=0,y=0):
self.pos=x
self.pos=y

in the point definition you can now initialise a point using:

x=point(1,2)

and if you don’t specify a value then a (0,0) point is constructed by default. There is also a destructor operator, indexing operator, string slicing operator and so on, all of which can be overloaded.

<ASIN:3540739157>

<ASIN:1934356271>

<ASIN:3642024742>

<ASIN:1430218436>

<ASIN:B003JTHYUI@ALL>

<ASIN:1430219629>



Last Updated ( Wednesday, 21 July 2010 )
 
 

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