1. 访问控制
对于__开头的类变量或成员变量,python会认为其是私有的,将以两个"_"开头的类成员变量或对象变量名前加入"_“+类名,使得基于"__"开始的变量是无法被正常访问的
class MyClass:
classValue1 = 100
_classValu2 = 200
__classValue3 = 300
def __init__(self):
self.instanceValue1 = 100
self._instanceValue2 = 200
self.__instanceValue3 = 300
print "MyClass init"
def Hello(self):
print "MyClass Hello"
if __name__ == "__main__":
c = MyClass()
print dir(MyClass)
print dir(c)
输出结果:
C:\Python27\python.exe C:/pytest/main.py hello
MyClass init
['Hello', '_MyClass__classValue3', '__doc__', '__init__', '__module__', '_classValu2', 'classValue1']
['Hello', '_MyClass__classValue3', '_MyClass__instanceValue3', '__doc__', '__init__', '__module__', '_classValu2', '_instanceValue2', 'classValue1', 'instanceValue1']
2. 内置常量
如果python启动时没有-O选项,则内置的__debug__常量值为False,同样的,对于未开启-O选项的python, 其assert为FALSE
3. 只有一个元素的tuple
如果元祖中只有一个元素,则需要在后面加上一个逗号,否则认为是单一的元素,如下
t = (40)
print t 则输出的是40
应该改为 (40,)
则输出的为(40,)
4. list转换成string
By using
''.join
list1 = ['1', '2', '3']
str1 = ''.join(list1)
Or if the list is of integers, convert the elements before joining them.
list1 = [1, 2, 3]
str1 = ''.join(str(e) for e in list1)