Basic Python Tutorial (second Edition) Learning Notes Classes and Objects (chapter 7th)
Defining classes
Class Person:
def setName (self,name):
Self.name=name
def getName (self):
Return Self.name
Foo=person ()
Foo.setname (' AAA ')
Foo.getname ()
Foo.name
Foo.name= ' BBB '
Private property, preceded by a two underscore in the name.
Class Person:
def __setname (self,name):
Self.name=name
def __getname (self):
Return Self.name
Class Person:
Members=0 #供全部实例访问
def init (self):
Person.members+=1 #供全部实例访问
def setName (self,name):
Self.name=name
def getName (self):
Return Self.name
__metaclass__=type
Class Filter:
def init (self):
Self.blocked=[]
def filter (self, sequence):
return [x for x in sequence if x not in self.blocked]
Class Spamfilter (Filter): #子类扩展了超类
def init (self):
self.blocked=[' SPAM '] #重写了init方法
Issubclass (Spamfilter, Filter) ==> True
spamfilter.__bases__ ==> (<class __main__. Filter at 0x0000000002e511c8>,)
Isinstance (foo, spamfilter) ==> False
Hasattr (foo, ' name ') ==> True
View all stored values within an object, using the __dict__ property.
foo.__dict__ ==> {' name ': ' AAA '}
Basic Python Tutorial (second Edition) Learning Notes Classes and Objects (chapter 7th)