Static methods in Python are distinguished from class member methods

Source: Internet
Author: User

The "Static Methods" and "class member methods" in the class declaration have been so confusing in the course of learning python that they briefly summarize the similarities and differences between the two declarations and their functions, as follows:

#-*-Coding:utf-8-*-

Class Test (object):

value1 = ' value1 ' #定义类变量value1


def __init__ (self):

self.value2 = ' value2 ' #在构造函数中定义实例变量value2


@staticmethod

def method1 ()

print ' static method, cannot invoke value1, value2 '


@classmethod #可省略

def method2 ()

print ' class member method, you can call value1, you can not call value2 '


This article is from "Life Beyond" blog, please be sure to keep this source http://cnlinux.blog.51cto.com/6112794/1599385

Static methods in Python are distinguished from class member methods

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.