Python uses adorner @property to turn a method into an instance of an attribute

Source: Internet
Author: User
This article mainly for you in detail the python with the adorner @property method into a feature example, interested in the small partners can refer to

#-*-Coding:utf-8-*-"" "Created on Sun Nov 23:19:03 @author: Toby" "" #知识点: Use the adorner @property to turn the method into an attribute class province:    Memo = ' One of China\ ' s provinces ' #静态字段         def __init__ (self,name,capital,leadership): Self        . Name = name #动态字段 self        . Capital = Capital #动态字段 self        . Leadership = Leadership #动态字段             def Sports (self): #定义一个动态方法, the class cannot access the dynamic method        print self. Name + ' The Sports meeting '         #把方法变成一个特性    @property #自带的装饰器    def Bar (self):        print self. Name        return ' somthing ' #也是可以有一个返回值的         #实例化两个对象, the object names are: HB, SDHB = Province (' Hebei ', ' Shjiazhuang ', ' Liyang ') SD = Province (' Shandong ', ' Jinan ', ' Angshenghui ') #通过对象访问这个属性, the Access form of the method becomes the Access form of the Access field print HB. Bar

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.