坑一

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

新式类与经典类问题

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中俩整数相除的结果最少保留一位小数(整除一位小数,非整除该几位就几位)

 

posted on 2016-12-13 10:13  23云恋49枫  阅读(282)  评论(1编辑  收藏  举报