heidsoft / cloud-bigdata-book

write book
56 stars 33 forks source link

Python Learn1 #30

Closed heidsoft closed 5 years ago

heidsoft commented 7 years ago

Python Learn1

Python中为什么推荐使用isinstance来进行类型判断?而不是type

Python在定义变量的时候不用指明具体的的类型,解释器会在运行的时候会自动检查变量的类型,并根据需要进行隐式的类型转化。因为Python是动态语言,所以一般情况下是不推荐进行类型转化的。比如"+"操作时,如果加号两边是数据就进行加法操作,如果两边是字符串就进行字符串连接操作,如果两边是列表就进行合并操作,甚至可以进行复数的运算。解释器会在运行时根据两边的变量的类型调用不同的内部方法。当加号两边的变量类型不一样的时候,又不能进行类型转化,就会抛出TypeError的异常。

但是在实际的开发中,为了提高代码的健壮性,我们还是需要进行类型检查的。而进行类型检查首先想到的就是用type(),比如使用type判断一个int类型。

import types
if type(1) is types.Integer:
    print('1是int类型')
else:
    print('1不是int类型')

上面的程序会输出:1是int类型

我们在types中可以找到一些常用的类型,在2.7.6中显示的结果:

types.BooleanType              #  bool类型
types.BufferType               #  buffer类型
types.BuiltinFunctionType      #  内建函数,比如len()
types.BuiltinMethodType        #  内建方法,指的是类中的方法
types.ClassType                #  类类型
types.CodeType                 #  代码块类型
types.ComplexType              #  复数类型
types.DictProxyType            #  字典代理类型
types.DictType                 #  字典类型
types.DictionaryType           #  字典备用的类型
types.EllipsisType
types.FileType                 #  文件类型
types.FloatType                #  浮点类型
types.FrameType
types.FunctionType             #  函数类型
types.GeneratorType       
types.GetSetDescriptorType
types.InstanceType             #  实例类型
types.IntType                  #  int类型
types.LambdaType               #  lambda类型
types.ListType                 #  列表类型
types.LongType                 #  long类型
types.MemberDescriptorType
types.MethodType               #  方法类型
types.ModuleType               #  module类型
types.NoneType                 #  None类型
types.NotImplementedType
types.ObjectType               #  object类型
types.SliceTypeh
types.StringType               #  字符串类型
types.StringTypes     
types.TracebackType   
types.TupleType                #  元组类型
types.TypeType                 #  类型本身
types.UnboundMethodType
types.UnicodeType    
types.XRangeType

在Python 3中,类型已经明显减少了很多

types.BuiltinFunctionType
types.BuiltinMethodType
types.CodeType
types.DynamicClassAttribute
types.FrameType
types.FunctionType
types.GeneratorType
types.GetSetDescriptorType
types.LambdaType
types.MappingProxyType
types.MemberDescriptorType
types.MethodType
types.ModuleType
types.SimpleNamespace
types.TracebackType
types.new_class
types.prepare_class

但是我们并不推荐使用type来进行类型检查,之所以把这些类型列出来,也是为了扩展知识面。那为什么不推荐使用type进行类型检查呢?我们来看一下下面的例子。

import types
class UserInt(int):
    def __init__(self, val=0):
        self.val = int(val)

i = 1
n = UserInt(2)
print(type(i) is type(n))

上面的代码输出:False

这就说明i和n的类型是不一样的,而实际上UserInt是继承自int的,所以这个判断是存在问题的,当我们对Python内建类型进行扩展的时候,type返回的结果就不够准确了。我们再看一个例子。

class A():
    pass

class B():
    pass

a = A()
b = B()

print(type(a) is type(b))

代码的输出结果: True

type比较的结果a和b的类型是一样的,结果明显是不准确的。这种古典类的实例,type返回的结果都是一样的,而这样的结果不是我们想要的。对于内建的基本类型来说,使用tpye来检查是没有问题的,可是当应用到其他场合的时候,type就显得不可靠了。这个时候我们就需要使用isinstance来进行类型检查。

isinstance(object, classinfo) object表示实例,classinfo可以是直接或间接类名、基本类型或者有它们组成的元组。

>>> isinstance(2, float)
False
>>> isinstance('a', (str, unicode))
True
>>> isinstance((2, 3), (str, list, tuple))
True

JSON encoding and decoding with Python

JSON conversion examples

Convert JSON to Python Object (Dict) To convert JSON to a Python dict use this:

import json

json_data = '{"name": "Brian", "city": "Seattle"}'
python_obj = json.loads(json_data)
print python_obj["name"]
print python_obj["city"]

Convert JSON to Python Object (List) JSON data can be directly mapped to a Python list.

import json

array = '{"drinks": ["coffee", "tea", "water"]}'
data = json.loads(array)

for element in data['drinks']: print element Convert JSON to Python Object (float) Floating points can be mapped using the decimal library.

import json
from decimal import Decimal

jsondata = '{"number": 1.573937639}'

x = json.loads(jsondata, parse_float=Decimal)
print x['number']

Convert JSON to Python Object (Example) JSON data often holds multiple objects, an example of how to use that below:

import json

json_input = '{"persons": [{"name": "Brian", "city": "Seattle"}, {"name": "David", "city": "Amsterdam"} ] }'

try:
    decoded = json.loads(json_input)

    # Access data
    for x in decoded['persons']:
        print x['name']

except (ValueError, KeyError, TypeError):
    print "JSON format error"

Convert Python Object (Dict) to JSON If you want to convert a Python Object to JSON use the json.dumps() method.

import json
from decimal import Decimal

d = {}
d["Name"] = "Luke"
d["Country"] = "Canada"

print json.dumps(d, ensure_ascii=False)
 #```

result {"Country": "Canada", "Name": "Luke"}
Converting JSON data to Python objects 
JSON data can be converted (deserialized) to Pyhon objects using the json.loads() function.  A table of the mapping:

JSON    Python
object  dict
array   list
string  str
number (int)    int
number (real)   float
true    True
false   False
null    None
Pretty printing

If you want to display JSON data you can use the json.dumps() function.

import json

json_data = '{"name": "Brian", "city": "Seattle"}' python_obj = json.loads(json_data) print json.dumps(python_obj, sort_keys=True, indent=4)