Python class variables

Source: Internet
Author: User
Tags shallow copy

#!/usr/bin/env python#_*_coding:utf8_*_classpeople (object): Infor='Test Info'#class VariablesInfor_dic = {        "name":'Dodo',        " Age": 30    }        #dictionaries can be shared, shallow copy knowledge    def __init__(self,name,age,job):#constructor method to implement parameter initializationSelf.name =name self.__age= Age#private variables, not externally callable, can only be called inside the class, cannot be changed externallySelf.job =Jobdef __breath(self):#Private method, cannot be called externally, can only be called inside class        Print('{} is Breathing .....'. Format (self.name))defget_age (self,target):iftarget = =' Age':            Print("age:{}". Format (self.__age))#calling private variables internally        elifTarge = ='W.H.O.':            Print('None ...')        Else:            Print("Hahahahhahahaha")    defWalk (self):Print("{} walking ....". Format (self.name)) self.__breath()#calling private methods internally    defTalk (self):Print("{} talking ....". Format (self.name))if __name__=='__main__': P1= People ("Dodo", 23,'Xiguan')    Print('p1.name:{}'. Format (p1.name))#print (' p1.age:{} '. Format (P1.get_age ("Age" ))P1.get_age (' Age') P1.walk () P1.talk ()#P1.__age External cannot access private variables, if you really want to access the P1._people__ageP1._people__age = 30#It is generally not recommended to access private variables like this    Print(' Age', P1._people__age)Print('name', P1.name)Print('Job', P1.job)

Python class variables

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.