上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Everything Is an Object in Python Objects are Python's abstraction for data. All data in a Python program is represented by objects or by relations between objects. ·Every object has an identity, a type, a value. An object's identity never changes once it has been created 11
Everything Is an Object in Python • Objects are Python's abstraction for data. • All data in a Python program is represented by objects or by relations between objects. • Every object has – an identity, – a type, – a value. • An object's identity never changes once it has been created 11
上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY “is”and id0 ·The"is"operator >>> x=5 >>> Y Y+1 compares the identity >>>Y=x >>> Y >>>id(x) 6 of two objects; >>> x is y ·The id()function 10411280 False returns an integer >>>id(y) >>> id(x) representing its 10411280 10411280 identity >>x is y >>>id(y) 10411268 True 12
“is” and id() • The "is" operator compares the identity of two objects; • The id() function returns an integer representing its identity >>> x = 5 >>> y = x >>> id(x) 10411280 >>> id(y) 10411280 >>> x is y True 12 >>> y = y + 1 >>> y 6 >>> x is y False >>> id(x) 10411280 >>> id(y) 10411268
上游充通大 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Type and Value: An object's type is unchangeable. An object's type determines the operations that the object supports and also defines the possible values for objects of that type. The value of some objects can change. Mutable objects 13
Type and Value: • An object’s type is unchangeable. • An object’s type determines the operations that the object supports and also defines the possible values for objects of that type. • The value of some objects can change. – Mutable objects 13
上游充通大学 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Mutable Objects vs Immutable Objects ·Objects whose value Mutable Immutable can change are said to be mutable; lists numbers ·Objects whose value dictionaries strings is unchangeable once tuple they are created are called immutable. An object's mutability is determined by its tvpe. 14
Mutable Objects vs Immutable Objects • Objects whose value can change are said to be mutable; • Objects whose value is unchangeable once they are created are called immutable. • An object's mutability is determined by its type. 14 Mutable Immutable lists numbers dictionaries strings tuple
上游充通大学 ParisTech SHANGHAI JIAO TONG UNIVERSITY INSTITUT DES SCIENCES ET TECHNOLOGIES PARIS INSTITUTE OF TECHNOLOGY Mutable Objects vs Immutable Objects >>>11=[0,1,2] >>ss abc >>> id(11) >>id(ss) 12210904 11564544 >>> mm=11 >>> ss ss "def >>> id (mm) >>> id(ss) 12210904 12208544 >>>mm[0] =9 >>>tt=ss[:-1] >>> >>> 七t >>> mm is 11 fedcba' True >>id(tt) >>>11 12208864 [9,1,2] >>>s1=ss[:3] 15
Mutable Objects vs Immutable Objects >>> ll = [0,1,2] >>> id(ll) 12210904 >>> mm = ll >>> id(mm) 12210904 >>> mm[0] = 9 >>> >>> mm is ll True >>> ll [9, 1, 2] >>> ss = "abc" >>> id(ss) 11564544 >>> ss = ss + "def" >>> id(ss) 12208544 >>> tt = ss[::-1] >>> tt 'fedcba' >>> id(tt) 12208864 >>> s1 = ss[:3] 15