Python member methods, class methods, and static methods

Source: Internet
Author: User
#! /Usr/bin/Python #-*-coding: UTF-8-*-Class A: def f_method (self, X): Print "% s, % s" % (self, x) @ staticmethod def f_smethod (x): Print "% s" % (x) @ classmethod def f_cmethod (CLS, X): print '% s, % s' % (CLS, X) A = a (). f_smethod (1). f_smethod (1). f_cmethod (1). f_cmethod (1). f_method (1)

Returned results:

11 _ main _. A, 1 _ main _. A, 1 <__ main _. A instance at 0x7f05613b4c68>, 1

Member method: instance objects must be called. The self object is an instance object. During the call, A is implicitly passed to the self object.

Class Method: You can use not only instance objects, but also class objects. The CLs parameter is a class object.

Static Method: You can call not only instance objects, but also class objects. Different from member methods and class methods, it requires instance objects or
The class object is passed in as a hermit parameter. Static methods are similar to common non-class methods and can be used as some common methods encapsulated in the form of classes.

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.