Python Basic Learning 1-descriptors

Source: Internet
Author: User

#!/usr/bin/env python#-*-coding:utf-8-*-#描述符就是将某种特殊类型的类的实例指派给另一个类的属性 # Special type means implemented # __get__ (Self,instance,owner), # __ Set__ (Self,instance,value), # __delete__ (Self,instance) Three methods at least one class Mydecriptor: #描述符类 def __get__ (self, instance, Owner): Print ("__get__", self, instance, owner) def __set__ (self, instance, value): Print ("__set__", Self, instance, value) def __delete__ (self, Instance): Print ("__delete__", Self, Instance) class Test:x=mydecriptor ( ) t1= Test () print (t1.x) t1.x= "X-man" #===== instance the temperature conversion of Fahrenheit class Celsius:def __init__ (self,value=26) according to the set degree Celsius: self.        Value=value def __get__ (self, instance, owner): Return Self.value def __set__ (self, instance, value): Self.value = Valueclass fahrenheit:def __get__ (self, instance, owner): Return instance.cel*1.8+32 def __se    T__ (self, instance, value): instance.cel= (float) (value-32)/1.8class temperature: #温度类的两个属性分别设置的摄氏度标识符类和华氏度标识符类 Cel=celsius (Fah=fahrenheit)() tem = temperature () print (tem.cel) print (Tem.fah) 

Python Basic Learning 1-descriptors

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.