Python object-oriented grooming--------1. Classes and Instance Properties

Source: Internet
Author: User

1. Properties of the class :

#/usr/bin/env#coding = Utf-8classChinese:#The properties of the class are in thisCountry =' China'    def __init__(self, name):#The properties of the instance are in =namedefPlay_ball (self, Ball):Print('%s is playing%s'%(, ball))#View PropertiesPrint('Japan'Print( P1=chinese ('Alex')Print(P1.__dict__)Print( Propertychinese.dang='Party'Print(Chinese.dang)Print(P1.dang)#Delete PropertydelChinese.dangdelChinese.countryPrint(Chinese.__dict__)#print ( chinajapan{'name':'Alex'}japan Party Party {'__module__':'__main__','__init__': <function Chinese.__init__At 0x0000028b584a66a8>'Play_ball': <function Chinese.play_ball at 0x0000028b584a6620>, '__dict__': <attribute'__dict__'Of'Chinese'Objects>,'__weakref__': <attribute'__weakref__'Of'Chinese'Objects>,'__doc__': None} adds function properties:#Define a functiondefEat_food (self,food):Print('%s is eating%s'%(,food))#give this function to the ('Excrement')#TestdefTest (self):Print('Test') Chinese.play_ball=Testp1.play_ball ()


Alex is eating the crap test.

2. Properties of the instance:

#/usr/bin/env#coding = Utf-8classChinese:country=' China'    def __init__(self,name): (self,ball):Print('%s is playing%s'%(,ball)) P1=chinese ('Alex')Print(P1.__dict__)#IncreaseP1.age=18Print(P1.__dict__)Print(p1.age)#do not modify the underlying property dictionary#p1.__dict__[' sex ']= ' male '#print (p1.__dict__)#print (


{' name ': ' Alex '} {' name ': ' Alex ', ' age ': +}18{' name ': ' Alex ', ' age ':}19   

Python object-oriented grooming--------1. Classes and Instance Properties

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: 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.