"Python Learning Notes" 7. More abstract

Source: Internet
Author: User

"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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.