Introduction to Python's static and dynamic methods

Source: Internet
Author: User
This article mainly for everyone in-depth analysis of the Python static method and dynamic methods of the relevant information, with a certain reference value, interested in small partners can refer to

#-*-Coding:utf-8-*-"" "Created on Sun Nov 23:19:03 @author: Toby" "" #知识点: Static method and dynamic method # static method belongs to class # dynamic method belongs to object class Provin CE:    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 '         #定义一个静态方法, the implementation class can access this method    @staticmethod #第一步, add a self-brought decorator    def Foo (): #第二步, remove the shelf        print ' anti-corruption activities ' #实例化两个对象, the object names are: HB, SDHB = Province (' Hebei ', ' Shjiazhuang ', ' Liyang ') SD = Province (' Shandong ', ' Jinan ', ' Angshenghui ') #对象访问动态方法 (Note: Class cannot access dynamic methods) Hb.sports () sd.sports () #通过类名访问静态方法Province. Foo () #那么, does the object have access to static methods? The answer is yes to HB. Foo ()

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.