• python 版本问题大全


    坑一

    一下午的时间又让这个不是问题的问题给白白给浪费了,此片文章仅仅纪念一下浪费掉的宝贵时间

    新式类与经典类问题

    class qwe:
        def __init__(self, name):
            self.name = name
    
        @property
        def f4(self):
            temp = self.name
            return temp
    
        @f4.setter
        def f4(self, value):
            print '打印的东西'
            self.name = value
    
    a = qwe("111")
    a.f4 = 456
    print(a.f4) 

    在特性的使用过程中,在2.7的版本中如果类不继承object的话,@property将不起作用

    坑二

      Python3中内置类型bytes和str用法及byte和string之间各种编码转换 Python 3最重要的新特性大概要算是对文本和二进制数据作了更为清晰的区分。文本总是Unicode,由str类型表示,二进制数据则由bytes类型表示。

    bytes类型在python3中才新加入的,在python2中没有这个函数

    python2中

    a = 'hello'
    print type(a)
    print type(bytes(a))
    print type(str(a))
    
    
    <type 'str'>
    <type 'str'>
    <type 'str'>
    

    python3中

    b = 'hello'
    print(type(b))
    print(type(bytes(b,encoding='utf8')))
    print(type(str(b)))
    
    
    <class 'str'>
    <class 'bytes'>
    <class 'str'>
    

    坑三

    人们常常会忽视Python 3在整数除法上的改动,而导致在代码迁移的过程中出现错误,而且在面试题中也有出现,所以值得总结一下

    import platform
    print (platform.python_version())
    
    print ('=========================')
    print ('3 / 2 =', 3 / 2)
    print ('3 // 2 =', 3 // 2)
    print ('3 % 2 =', 3 % 2)
    print ('=========================')
    print ('3 / 2.0 =', 3 / 2.0)
    print ('3 // 2.0 =', 3 // 2.0)
    print ('3 % 2.0 =', 3 % 2.0)
    print ('=========================')
    print ('3.0 / 2.0 =', 3.0 / 2.0)
    print ('3.0 // 2.0 =', 3.0 // 2.0)
    print ('3.0 % 2.0 =', 3.0 % 2.0)
    print ('=========================')

    python 2.7.13

    2.7.13
    =========================
    3 / 2 = 1
    3 // 2 = 1
    3 % 2 = 1
    =========================
    3 / 2.0 = 1.5
    3 // 2.0 = 1.0
    3 % 2.0 = 1.0
    =========================
    3.0 / 2.0 = 1.5
    3.0 // 2.0 = 1.0
    3.0 % 2.0 = 1.0
    =========================
    

    python 3.2.0

    3.2.0
    =========================
    3 / 2 = 1.5
    3 // 2 = 1
    3 % 2 = 1
    =========================
    3 / 2.0 = 1.5
    3 // 2.0 = 1.0
    3 % 2.0 = 1.0
    =========================
    3.0 / 2.0 = 1.5
    3.0 // 2.0 = 1.0
    3.0 % 2.0 = 1.0
    =========================

    由上面的执行结果可以看出来,两者最主要的不同在于整数除法上的改动,在python2系列中俩个整数间相除得到的结果为整数,而在python3中俩整数相除的结果最少保留一位小数(整除一位小数,非整除该几位就几位)

  • 相关阅读:
    kubernetes部署1.15.0版本
    搭建时间服务器
    创建mysql容器
    制作带sshd功能的centos镜像
    容器操作
    镜像制作
    elk日志系统
    k8s基于canel的网络策略
    k8s的flannel网络插件配置
    k8s搭建WebUI--Dashborad管理界面
  • 原文地址:https://www.cnblogs.com/luxiaojun/p/6165518.html
Copyright © 2020-2023  润新知