2010年6月25日 星期五

what is __init__? and what are classes and objects?

what is __init__? and what are classes and objects?

init is an abbreviation for initialization.

For instance if I have a Point class... that has two objects which are the x and y coordinates i can do this:


class Point():
pass

point = Point()
point.x = 3.0
point.y = 4.0


so instead you can do this:


class Point():
def __init__(self, x=0,y=0):
self.x = x
self.y = y


now you can make a point like this:

point = Point(3,4)

or

point = Point() # which gives you a point of default 0,0 .

Reference :
http://ubuntuforums.org/archive/index.php/t-540438.html

0 意見:

張貼留言