编程札记

P

Python3 First cgi 程序

Python3 First cgi 程序 #!/usr/bin/python print ("Content-type:text/html\r\n\r\n") print ('<html>') print ('<head>') print ('<title>Hello Word - First CGI Program</title>') print ('</head>') print ('<body>') print ('<h2>Hello Word! This is my first CGI program</h2>') print ('</body>') print ('</html>')

阅读全文 ↗
P

Python3 正则表达式 查找和替换

Python3 正则表达式 查找和替换 #!/usr/bin/python3 import re phone = "2004-959-559 # This is Phone Number" # Delete Python-style comments num = re.sub(r'#.*$', "", phone) print ("Phone Num : ", num) # Remove anything other than digits num = re.sub(r'\D', "", phone) print ("Phone Num : ", num)

阅读全文 ↗
P

Python3 正则表达式match vs search

Python3 正则表达式match vs search #!/usr/bin/python3 import re line = "Cats are smarter than dogs"; matchObj = re.match( r'dogs', line, re.M|re.I) if matchObj: print ("match --> matchObj.group() : ", matchObj.group()) else: print ("No match!!") searchObj = re.search( r'dogs', line, re.M|re.I) if searchObj: print ("search --> searchObj.group() : ", searchObj.group()) else: print ("Nothing found!!")

阅读全文 ↗
P

Python3 正则表达式search方法

Python3 正则表达式search方法 #!/usr/bin/python3 import re line = "Cats are smarter than dogs"; searchObj = re.search( r'(.*) are (.*?) .*', line, re.M|re.I) if searchObj: print ("searchObj.group() : ", searchObj.group()) print ("searchObj.group(1) : ", searchObj.group(1)) print ("searchObj.group(2) : ", searchObj.group(2)) else: print ("Nothing found!!")

阅读全文 ↗
P

Python3 正则表达式match方法

Python3 正则表达式match方法 #!/usr/bin/python3 import re line = "Cats are smarter than dogs" matchObj = re.match( r'(.*) are (.*?) .*', line, re.M|re.I) if matchObj: print ("matchObj.group() : ", matchObj.group()) print ("matchObj.group(1) : ", matchObj.group(1)) print ("matchObj.group(2) : ", matchObj.group(2)) else: print ("No match!!")

阅读全文 ↗
P

Python3 For Else 语句

Python3 For Else 语句 #!/usr/bin/python3 numbers = [11,33,55,39,55,75,37,21,23,41,13] for num in numbers: if num%2 == 0: print ('the list contains an even number') break else: print ('the list doesnot contain even number')

阅读全文 ↗
P

Python3 隐藏数据

Python3 隐藏数据 #!/usr/bin/python3 class JustCounter: __secretCount = 0 def count(self): self.__secretCount += 1 print (self.__secretCount) counter = JustCounter() counter.count() counter.count() print (counter.__secretCount)

阅读全文 ↗
P

Python3 方法重载

Python3 方法重载 #!/usr/bin/python3 class Vector: def __init__(self, a, b): self.a = a self.b = b def __str__(self): return 'Vector (%d, %d)' % (self.a, self.b) def __add__(self,other): return Vector(self.a + other.a, self.b + other.b) v1 = Vector(2,10) v2 = Vector(5,-2) print (v1 + v2)

阅读全文 ↗
P

Python3 方法覆盖

Python3 方法覆盖 #!/usr/bin/python3 class Parent: # define parent class def myMethod(self): print ('Calling parent method') class Child(Parent): # define child class def myMethod(self): print ('Calling child method') c = Child() # instance of child c.myMethod() # child calls overridden method

阅读全文 ↗
P

Python3 类继承

Python3 类继承 #!/usr/bin/python3 class Parent: # define parent class parentAttr = 100 def __init__(self): print ("Calling parent constructor") def parentMethod(self): print ('Calling parent method') def setAttr(self, attr): Parent.parentAttr = attr def getAttr(self): print ("Parent attribute :", Parent.parentAttr) class Child(Parent): # define child class def __init__(self): print ("Calling child constructor") def childMethod(self): print ('Calling child method') c = Child() # instance of child c.childMethod() # child calls its method c.parentMethod() # calls parent's method c.setAttr(200) # again call parent's method c.getAttr() # again call parent's method

阅读全文 ↗
P

Python3 销毁对象

Python3 销毁对象 #!/usr/bin/python3 class Point: def __init__( self, x=0, y=0): self.x = x self.y = y def __del__(self): class_name = self.__class__.__name__ print (class_name, "destroyed") pt1 = Point() pt2 = pt1 pt3 = pt1 print (id(pt1), id(pt2), id(pt3)); # prints the ids of the obejcts del pt1 del pt2 del pt3

阅读全文 ↗
P

Python3 内建类属性

Python3 内建类属性 #!/usr/bin/python3 class Employee: 'Common base class for all employees' empCount = 0 def __init__(self, name, salary): self.name = name self.salary = salary Employee.empCount += 1 def displayCount(self): print ("Total Employee %d" % Employee.empCount) def displayEmployee(self): print ("Name : ", self.name, ", Salary: ", self.salary) emp1 = Employee("Zara", 2000) emp2 = Employee("Manni", 5000) print ("Employee.__doc__:", Employee.__doc__) print ("Employee.__name__:", Employee.__name__) print ("Employee.__module__:", Employee.__module__) print ("Employee.__bases__:", Employee.__bases__) print ("Employee.__dict__:", Employee.__dict__ )

阅读全文 ↗