Python Classes and objects

             Python is fully object-oriented: we can define our own classes, inherit from our own or built-in classes,
 and instantiate the classes we have defined.
A Python class starts with the reserved word class, followed by the class name. 

A Simple Python Class

class Point(object):
	pass

1. The name of this class is Point, and it doesn't inherit from any other class. 

2. This class doesn't define any methods or attributes, but syntactically, there needs to be something in the 
   definition, so you use pass.

3. Everything in a class is indented, just like the code within a function, if statement, for loop, and while loop. 

Here the pass statement in Python is like an empty set of braces ({}) in Java or C.

A class efinition can also like this:

class Point(object):
    """represents a point in 2-D space"""

Here the body is a docstring that explains what the class is for.
Let us see what will be the output when we will print the calss Point.

>>> print Point
<class '__main__.Point'>

Since the class Point is defined at the top level, its “full name” is __main__.Point.

To create a Point,call Point as if it were a function.

>>> blank = Point()
>>> print blank
<__main__.Point object at 0xb7785a6c>

The output will be a return value,that is a reference to a Point object, which we assign to blank. Creating a new object is
 called instantiation, and the object is an instance of the class.

We can also assign values to an instance using dot notation.For example:

>>> blank.x = 3.0
>>> blank.y = 4.0

To read the value of an attribute we will use the following syntax:

>>> print blank.y
4.0
>>> x = blank.x
>>> print x
3.0

We can also define functions inside the class as same as the attributes.
ex:-

class Rectangle(object):
    """represent a rectangle.
       attributes: width, height, corner.
    """
To represent a rectangle, you have to instantiate a Rectangle object and assign values to the attributes:

box = Rectangle()
box.width = 100.0
box.height = 200.0
box.corner = Point()
box.corner.x = 0.0
box.corner.y = 0.0

The figure shows the state of this object:

An object that is an attribute of another object is embedded.

Here find_center takes a Rectangle as an argument and returns a Point that contains the coordinates of the center of the Rectangle:

def find_center(box):
    p = Point()
    p.x = box.corner.x + box.width/2.0
    p.y = box.corner.y + box.height/2.0
    return p

Here is an example that passes box as an argument and assigns the resulting Point to center:

>>> center = find_center(box)
>>> print_point(center)
(50.0, 100.0)
Advertisements

About ramyabkrishna

I am Ramya B Krishna doing MCA at GEC Thrissur. I am now in the process of taking a Leap into the world of Linux and hoping to do a few projects as well.

Posted on July 4, 2011, in Uncategorized. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: