Basic Python Tutorial (second Edition) Learning Notes Classes and Objects (chapter 7th)

Source: Internet
Author: User

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)

Related Article

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.