"Python Learning Notes" 7. More abstract
- class is defined as executing a code block
- A raw instance is saved in memory and can be accessed through the class name
- Class is created by creating a copy of the original instance, and all member variables are bound to the original instance
- You can unbind the original instance by modifying the instance variable
- Self represents a reference to the current instance
- Member variables are also known as attributes
- __BASES__: base class
- __class__: Object Type
- __DICT__: All Features
- The interface of Python does not explicitly define which methods an object must contain, as long as the object conforms to the current interface and can be called
- You can create a variable on an object by assigning a value
#!/usr/bin/env python#-*-coding:utf-8-*-# Specify the source code file encoding, python defaults to ascii__metaclass__ = type # OK to use the modern classes class Person:de F SetName (self, name): # Self is a reference to the current instance Self.name = Name def getName (self): return self.name def greet (self): print "Hello, world! I ' m%s. "% Self.name def __inaccessible (self): # Private Function, the outside world can not access the print" Bet you can t see me ... "Def _inaccessib Le (self): # will not be exported by import print "Bet you can t see me ..." foo = person () # Create Object bar = Person () foo.setname ("foo") bar.se Tname ("bar") print foo.getname () print bar.getname () Foo.greet () Bar.greet () #foo. __inaccessible () foo._inaccessible () Print "------------------------" "Class Membercounter:print" class Membercounter begin defined "Members = 0 members2 = 0 def init (self): Membercounter.members + = 1 Membercounter.members2 + = 1M1 = Membercounter () m1 . init () m2 = Membercounter () m2.init () print "----" print "m1.members", M1.membersprint "M2.members", M2.membersprint " MembeRcounter.members ", membercounter.membersprint"----"Print" Modify M1.members "M1.members= ' M1 ' print"----"print" M1.members ", M1.membersprint" M2.members ", M2.membersprint" Membercounter.members ", membercounter.membersprint"---- "Print" Modify membercounter.members "membercounter.members =" membercounter "print"----"print" m1.members ", M1.membersprint "M2.members", M2.membersprint "Membercounter.members", membercounter.membersprint "----" print " Create M3 "m3=membercounter () print"----"print" m1.members ", M1.members," M1.members2 ", M1.members2print" M2.members ", M2.members, "M2.members2", M2.members2print "M3.members", M3.members, "M3.members2", M3.members2print " Membercounter.members ", Membercounter.members," Membercounter.members2 ", membercounter.members2print"----"print" Modify Membercounter.members2 "membercounter.members2=" members2 "print"----"print" m1.members ", M1.members," M1.members2 ", M1.members2print" M2.members ", M2.members," M2.members2 ", M2.members2print" M3.members ", M3.membERs, "m3.members2", M3.members2print "Membercounter.members", Membercounter.members, "Membercounter.members2", Membercounter.members2print "----" m3.members2= "m3.members2" membercounter.members2= "Members2 last" print "----" Print "M1.members", M1.members, "M1.members2", M1.members2print "M2.members", M2.members, "M2.members2", M2.members2print "M3.members", M3.members, "M3.members2", M3.members2print "Membercounter.members", Membercounter.members, "Membercounter.members2", membercounter.members2print "----" "Print"------------------ ---"Class filter:def init (self): self.blocked = [] # CREATE member variable def Filter (self, Sequence): return [x for X in sequence if x not in Self.blocked]class spamfilter (Filter): # inheritance, can be comma-delimited multiple base class def init (self): # overloaded SELF.BL ocked = [' SPAM ']f = Filter () f.init () print f.filter ([]) s = spamfilter () s.init () Print s.filter (["SPAM", "SPAM", "SPAM "," SPAM "," eggs "," bacom "]) print"---------------------"Class c:def Dis (self): self.C=1 print self.cc=c () C.dis () c.cc=3print "c.cc", C.ccprint "c.__dict__", C.__dict__print "c.__dict__", C.__dict__pri NT "filter.__dict__", Filter.__dict__print "f.__dict__", F.__dict__print "s.__dict__", S.__dict__print "m1.__dict__", M1.__dict__print "m2.__dict__", M2.__dict__print "m3.__dict__", M3.__dict__print "membercounter.__dict__", membercounter.__dict__
Python Learning note 7. More abstract