上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Create Array def Create Array (row,col):.If we want to Array [ change an line [ array element, for i in range (col): what happened? line.append(0) for i in range(row): >>>AA=Create Ar Array.append(line) ray(3,5) return Array >>>AA[0][2]=3 16
Create Array def Create_Array(row,col): Array = [] line = [] for i in range(col): line.append(0) for i in range(row): Array.append(line) return Array • If we want to change an array element, what happened? >>>AA=Create_Ar ray(3,5) >>>AA[0][2]=3 16
上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Create Array All lines refere to the same list! >>>id(AA[0]) 18822688 >>>id(AA[1]) 18822688 >>>id(AA[2]) line 18822688 Array:[Array[0],Array[1],Array[2]] 17
Create Array • All lines refere to the same list! >>> id(AA[0]) 18822688 >>> id(AA[1]) 18822688 >>> id(AA[2]) 18822688 17
上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Reference Counting ·How many other >>import sys objects are >>>a=1 referencing the >>sys.getrefcount (a) 241 current object >>> ,b=100001 >>sys.getrefcount (b) 2 >>>c =b >>c is b True >>sys.getrefcount (b) 3 18
Reference Counting • How many other objects are referencing the current object >>> import sys >>> a = 1 >>> sys.getrefcount(a) 241 >>> b = 100001 >>> sys.getrefcount(b) 2 >>> c = b >>> c is b True >>> sys.getrefcount(b) 3 18
上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Defining New Classes A class is a collection of methods,and methods are just functions. ·class<class-name>: <method-definitions> class <class-name>(base-class- name ) <method-definitions> 19
Defining New Classes • A class is a collection of methods, and methods are just functions. • class <class-name>: <method-definitions> • class <class-name> (base-classname ): <method-definitions> 19
上游充通大粤 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Class Point point.py Class def for a point. class Point: de f init (self,xVal 0,yVal 0): self.x xVal self.y yval def setvalue(self,xVal,yval): self.x xVal self.y yval def getvalue(self): return self.x,self.y def pp(self): print "("self.x "self.y ") 20
Class Point # point.py # Class def for a point. class Point: def __init__(self, xVal = 0, yVal = 0): self.x = xVal self.y = yVal def setValue(self, xVal,yVal): self.x = xVal self.y = yVal def getValue(self): return self.x, self.y def pp(self): print "(", self.x, ",", self.y,")" 20