Python's function usage __setattr__

Source: Internet
Author: User

#-*-coding:utf-8-*-#python#Xiaodeng#python's function usage __setattr__#http://www.cnblogs.com/hongfei/p/3858256.html
#Reconstruction method using __setattr__ functionclassFruit ():def __init__(Self,color,price): Self.__color=color self.__price= Pricedef __setattr__(Self,name,value):#Refactoring Method __setattr__ method, the method is not present by defaultSelf.__dict__[Name] = STR ('Xiaodeng,')+Str (value) #为了看出区别, specifically added str (' Xiaodeng ') in the __setattr__ methodif __name__=="__main__": Fruit= Fruit ("Blue", 10) PrintFruit#__main__. Fruit instance PrintFruit.__dict__ #{' _fruit__price ': ' xiaodeng,10 ', ' _fruit__color ': ' Xiaodeng,blue '} PrintFruit.__dict__. Get ("_fruit__price")#xiaodeng,10 #to set a new property for _fruit__priceFruit.__dict__["_fruit__color"] ='Red' PrintFruit.__dict__. Get ("_fruit__color")#Red #adding new properties and property valuesFruit.__dict__['name']='Xiaodeng' PrintFruit.__dict__ #{' _fruit__price ': ' xiaodeng,10 ', ' name ': ' Xiaodeng ', ' _fruit__color ': ' Red '}

Python's function usage __setattr__

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.