zoukankan      html  css  js  c++  java
  • The init method

    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.

     

     

    from Thinking in Python

  • 相关阅读:
    javaSE第十五天
    javaSE第十四天
    javaSE第十三天
    javaSE第十二天
    javaSE第十一天
    javaSE第十天
    javaSE第九天
    python011 Python3 字典
    python010 Python3 元组
    python009 Python3 列表
  • 原文地址:https://www.cnblogs.com/ryansunyu/p/4004170.html
Copyright © 2011-2022 走看看