python 学习-格式化 && class define

下面是我获取时间程序,值得注意的是 %y %Y得到结果是不同的,现在亲测的是:

%y ->18   %Y->2018

%H->3(hour) %y->Aug(month,I don't known)

dt = datetime.now()

l="now it is {:%Y-%m-%d %H:%M:%S}".format(dt)
------class defines------

class person:
    ''' tiis is a description'''
    def __init__(self,name,tel):
        self.name=name
        self.tel=tel
    def func(self) :
        return self.name + "'s phone is" + self.tel

class hobbit(person):
    def __init__(self, name, tel, sport):
        super().__init__(name, tel)
        self.sport=sport

    def get_hob(self):
        print(self.sport)

##
##p=person("shenhao", "234567")
##print(p.name)
##print(person. __doc__)
##string = p.func()
##print(string)

sc =  hobbit("sc","3456","pk")
 

值得注意的是:

1 init是构造函数,命名的必须按照这样,前后各两个下划线;

2 self是相当于this指针;

3 '''  "'相当于注释,但可以通过classname.__doc__(两个下划线)输出,也相当于类的成员,帅啊;

4 super().__init__(name, tel) #等价代码是person.__init__(self)

也就是说,父类的东西都被子类继承了;不知道跟有些博客上讲抽象类有什么关系????


版权声明:本文为qq_24328911原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。