码迷,mamicode.com
首页 > 其他好文 > 详细

The init method

时间:2014-10-02 18:06:23      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   io   ar   for   sp   

The init method is a special method that gets invoked when an object is instantiated. Its full name is __init__ (two underscore characters, followed byinit, and then two more underscores). An init method for the Time class might look like:

def __init__(self,hour=0,minute=0,second=0):
        self.hour = hour
        self.minute = minute
        self.second = second

It is common for the parameters of __init__ to have the same names as the attributes. The parameters are optional, so if you call Time with no arguments, you get the default values. If you provide one argument, it overrides hour. If you provide two arguments, they override hour and minute. And if you provide three arguments, they override all three default values.

bubuko.com,布布扣 

 

from Thinking in Python

The init method

标签:des   style   blog   http   color   io   ar   for   sp   

原文地址:http://www.cnblogs.com/ryansunyu/p/4004170.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!