Classes and object

Classes and object:
As an example, we will create a type called point that represents a point in two-dimensional space.
In mathematical notation, points are often written in parentheses with a comma separating the coordinates. For example,(0,0) represents the origin, and(x,y) represents the point x unit to the right and y units up from the origin.
There are several ways to represent points in Python:
1. We could store the coordinates separately in two variables,x, and y.
2. We could store the coordinates as elements in a list or tuple.
3. We could create a new type to represent a point as objects.

A user-defined type is also called a class. A class definition looks like this:
class Point(object):
“””Represent a point in 2-D space”””
This header indicates that the new class is a Point, which is a kind
of an object, which is a built-in type.
Defining a class named Point create a class object:

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

>>>blank =Point()
>>>print blank
<__main__.Point instance at 0xb7e9d3ac>

The return value 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.

Translate »