一.变量克隆 在js中经常会遇到将一个变量赋值给一个新的变量这种情况,这对于基本类型很容易去实现,直接通过等号赋值就可以了,对于引用类型就不能这样了.(注:像函数,正则也可以直接通过等号赋值) 这里我写了一个复制值的函数,可以进行深度复制,也能进行浅复制,要进行深度复制只需要将第二个参数设置为true即可 function clone(data,deep){ var cloneData = undefined; var data = arguments[0],deep = arguments[1
Python的变量类型 变量可以指定不同的数据类型,这些变量可以存储整数,小数或字符. 变量赋值 Python 中的变量赋值不需要类型声明 等号(=)用来给变量赋值,等号左边为变量值,等号右边是存储在变量中的值 eg: a = b = c = 1 a, b, c = 1, 2, "john" #两个整型对象1和2的分配给变量 a 和 b,字符串对象 "john" 分配给变量 c. 标准数据类型 Number(数字) string(字符串) list(列表) t
这里有两种方法.type 和isinstance import types aaa = 0 print type(aaa) if type(aaa) is types.IntType: print "the type of aaa is int" if isinstance(aaa,int): print "the type of aaa is int" bbb = 'hello' print type(bbb) if type(bbb) is types.Stri
在实际写程序中,经常要对变量类型进行判断,除了用type(变量)这种方法外,还可以用isinstance方法判断: #!/usr/bin/env pythona = 1b = [1,2,3,4]c = (1,2,3,4)d = {‘a‘:1,‘b‘:2,‘c‘:3}e = "abc"if isinstance(a,int): print "a is int"else: print "a is not int"if isinstance
代码中经常会有变量是否为None的判断,有三种主要的写法:第一种是`if x is None`:第二种是 `if not x:`:第三种是`if not x is None`(这句这样理解更清晰`if not (x is None)`) .如果你觉得这样写没啥区别,那么你可就要小心了,这里面有一个坑.先来看一下代码: ? 1 2 3 4 5 6 7 8 9 10 11 12 >>> x = 1 >>> not x False >>> x = [1] &