11-类和对象

time模块

  • 获取当前的时间戳
    • t = time.time()
  • 获取当前的时间元组
    • print(time.gmtime()) # 当前时间的时间元组,UTC时间
    • print(time.gmtime(1694090643)) # 时间戳=>时间元组
    • t2 = time.localtime(t)
    • print(time.localtime(t)) # 时间元组,本地时间
  • 时间元组转换为时间戳
    • print(time.mktime(t2))
  • 时间元组转换为时间字符串
    • print(time.strftime('%Y-%m-%d %H:%M:%S', t))
  • 时间字符串转换为时间元组
    • s2 = '2020-07-07 11:08:56'
    • t2 = time.strptime(s2, '%Y-%m-%d %H:%M:%S')
  • 输出格式化字符串
    • 时间元组格式化
      • print(time.asctime(time.localtime())) # Tue Jul 7 11:17:55 2020
    • 时间戳格式化
      • print(time.ctime(time.time())) # Tue Jul 7 11:19:17 2020
  • time.sleep()
    • time.sleep():暂停,会阻塞程序
    • time.sleep(0.5)

datetime模块

  • 获取当前时间的日期对象
    • d = datetime.datetime.now()
  • 创建指定的日期对象
    • d = datetime.datetime(year=2030, month=1, day=2)
    • print(d.year, d.month, d.day) # 2030 1 2
    • print(d.hour, d.minute, d.second) # 0 0 0
    • print(d.date(), d.time()) # 2030-01-02 00:00:00,日期+时间
  • 时间戳
    • print(d.timestamp()) # 时间戳, 1893513600.0
  • 格式化字符串输出
    • print(d.strftime('%y-%m-%d'))
  • 时间戳=>日期对象
    • print(datetime.datetime.fromtimestamp(d.timestamp()))
  • 时间差
    • d2 = datetime.timedelta(days=7, hours=10)
    • print(d+d2) # 七天之后的日期,2030-01-09 10:00:00
    • print(d-d2)

calendar模块

  • c = calendar.calendar(2020)

  • c = calendar.month(2020, 7)

  • calendar.isleap(2020)

  • print(calendar.leapdays(1900, 2020)) # 29,中间有多少个闰年

  • print(calendar.monthcalendar(2020, 7))

    • [
      [0, 0, 1, 2, 3, 4, 5], 
      [6, 7, 8, 9, 10, 11, 12], 
      [13, 14, 15, 16, 17, 18, 19], 
      [20, 21, 22, 23, 24, 25, 26], 
      [27, 28, 29, 30, 31, 0, 0]
      ]
      
  • print(calendar.monthrange(2020, 7)) # (2, 31),第一天星期三,一共31天

hashlib

  • md5加密:对字符串

    • m = hashlib.md5()
      # m.update('123456'.encode())  # 默认UTF-8编码
      m.update(b'ABCad,?123456')  # 也是字节型,不能是中文
      m2 = m.hexdigest()  # 获取十六进制的密文
      print(m2)  # e10adc3949ba59abbe56e057f20f883e,32个十六进制数组成的密文
      
  • 加密算法

    • md5:不可逆(不能解密),对称加密(明文和密文一一对应),cmd5.com破解
    • RSA:非对称加密,需要公钥和私钥
    • AES,DES:对称加密,可逆的,但是需要一个专门的key

面向对象

  • 面向过程

    • 侧重一步一步如何实现,按顺序或步依次执行,函数封装
  • 面向对象

    • 侧重的是解决这件事中的对象,类封装

      • class Dog:
            def smell(self):
                print('闻一闻')
            def lick(self):
                print('舔一舔')
            def bite(self):
                print('咬一咬')
        
        # 小狗对象
        dog = Dog()
        dog.smell()
        dog.lick()
        dog.bite()
        

类和对象

  • class HuaweiPhone:
        # 属性:变量
        color = '绿色'
        size = '6.2'
        price = 7000
        
        # 方法:函数
        # self:不是关键字,是存在于类中的函数里面的特殊形参
        # self:翻译=>自己
        # self:是指向当前类的一个对象,哪个对象调用了方法,则该self就是这个对象
        
        def call(self, name):
            print('打电话给', name, self.color)
            print('self:', id(self))
    
        def game(self):
            print('玩吃鸡')
            
    # 对象:通过类来创建对象(实例)
    # 一个类可以创建出任意多个不同的对象
    p40 = HuaweiPhone()
    # print(p40)  # <__main__.HuaweiPhone object at 0x0000024EF6DD3148>
    # print(p40.color, p40.size, p40.price)
    # p40.call('小李')
    # p40.game()
    # CPython 中 id() 函数用于获取对象的内存地址
    print('p40:', id(p40))  # p40: 1631955661448
    p40.call('小李')  # self: 1631955661448
    
    p50 = HuaweiPhone()
    print('p50:', id(p50))  # p50: 1422743253896
    p50.call('小李')  # self: 1422743253896
    
    • 类:作用是用来创建对象,类是对象的抽象,可以封装属性(变量)和方法(函数),一个类可以创建出任意多个不同的对象
    • 对象:通过类来创建,对象是类的具象
    • self
      • 不是关键字,是存在于类中的函数里面的特殊形参
      • 是指向当前类的一个对象,哪个对象调用了方法,则该self就是这个对象

构造函数和析构函数

  • 构造函数/构造方法
    • 1.会在创建对象时自动被调用
    • 2.作用是用于初始化属性
    • 3.如果不写init函数,内部会自动创建一个空init函数
  • 类属性和对象属性
  • 成员方法
  • 析构函数
    • 析构函数:在销毁对象时会被自动调用,一般可不写;可以用来主动释放内存
      • def del(self):
        • print('析构函数被调用')
原文地址:https://www.cnblogs.com/lotuslaw/p/14008630.html