python

List Seq table:

  • 正向输出:
    python a = [1,2,3] /* python2.7 */ print a /* python3.5 */ print (a)

输出结果:
[1,2,3]

  • 逆向输出:
a = a[::-1] #以逆向步调为1反向输出

输出结果:
[3,2,1]

  • 字符串反转,可以借用list 反转:
	a = '123'
	a = int(a[::-1])
	# a = 321 --> int

str类型操作:

  • 修改str里的值,可采用转换成list类型
    • 方法1:转成list
a = 'hello'	
b = list(a) # --> b = ['h','e','l','l','o']	
b[0] = 'l'	
# 再转回去str	
b = ''.join(b)  # --> 'lello'	
  • 方法2:切片
a = 'hello'	
b = a[-1:0] + 'l' + a[1:]	
# b = 'lello'	
  • 方法3:replace -->str函数
a = 'hello'	
a = a.replace(a[0],'l')	

装饰器:

@property

  1. 在函数、变量前面可加
  2. 私有的,不可调用会抛出异常。
    注:
  • _a__a 是私有
  • __a__ 这是魔法方法

Property 有两种属性:

  • setter: 在Get/Set 之类的函数时,声明时,可以不用写Get、Set,在函数调用时当作属性用:
  • dellter:

** 看下面例子:**

	class test:
		__a = 0
		__b = ' '
		
		def __init__(self):
			__a = 0
			__b = ' '
		def setTest(self,a,b):
			__a = a
			__b = b
			
		def getTestA(self):
			return __a
			
		def getTestB(self):
			return __b
			

如果要使用@Property:
只需要在函数前面写上@Property

	#将上面的代码改写成:
	@property
	def getTestA(self):
		return __a
		
		
	# 调用时
	Test t
	t.getTestA   # 这时 getTestA <==> getTestA() 

类型判断:

  • ** type关键字: **

    type用法: type(varName) 会返回一个类型说明

	 a = [1,2,3,4]
	 type(a)
	 
	 ## 此时输出:
	 ## <type 'list'>
  • ** isinstance 函数 **

instance用法:instance(varName,typeName)

a = [1,2,3,4]
instance(a,'list') 
原文地址:https://www.cnblogs.com/Kernel001/p/10108637.html