Python -笔记 -内部类&日期格式


本文适用对象:

python初级打怪~

本文介绍两个python的小知识点,内部类和日期格式转换


内部类

内部类顾名思义就是类内部包含的类,就叫内部类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Car: #外部类
class Door: # 内部类
def open(self):
print("open door")

class Wheel:
def run(self):
print("car run")

if __name__ =="__main__":
car = Car() #实例化外部类
backDoor = Car.Door() # 实例化内部类 第一种方法
frontDoor = car.Door() # 因为car已经实例化外部类,再次实例化Car的内部类 第二种方法
backDoor.open()
frontDoor.open()

以上代码列举了调用内部类的两种方法
第一种调用内部类的方法是:
通过外部类直接调用,不实例化外部类。
第二种调用内部类的方法是:
通过外部类的实例化对象调用


日期格式:

字符格式转换成日期格式

1
2
3
import datetime
datetime.datetime.strptime('2019-04-13', '%Y-%m-%d')
output:2019-04-13 00:00:00

六天后的时间

1
2
3
4
5
6
import datetime
now = datetime.datetime.strptime('2019-04-13', '%Y-%m-%d')
times = '2019-04-13'
endnow = now+datetime.timedelta(days=6)
print(endnow)
#output:2019-04-19 00:00:00

时间转换成字符串

1
2
3
4
5
6
import datetime
now = datetime.datetime.strptime('2019-04-13', '%Y-%m-%d')
times = '2019-04-13'
endnow = now+datetime.timedelta(days=6)
print(endnow.strftime('%Y-%m-%d'))
#output:2019-04-19


觉得不错的话,给点打赏吧 ୧(๑•̀⌄•́๑)૭



wechat pay



alipay

Python -笔记 -内部类&日期格式
http://yuting0907.github.io/2019/04/13/Python -笔记 -内部类&日期格式/
作者
yuting
发布于
2019年4月13日
许可协议