Python Class (vii)-Special member Method of class

Source: Internet
Author: User

    • __doc__

Used to represent descriptive information for a class

#-*-Coding:utf-8-*-__author__ = "MuT6 Sch01ar" class Person (object): "    This class is used to describe the"    def __init__ (self,name , age):        self.name = name        self.age =ageif __name__ = = ' __main__ ':    p = person (' John ', ')    print (p.__doc__)

Run results

    • __class__

Class used to represent the current object

#-*-Coding:utf-8-*-__author__ = "MuT6 Sch01ar" class Person (object): "    This class is used to describe the"    def __init__ (self,name , age):        self.name = name        self.age =ageif __name__ = = ' __main__ ':    p = person (' John ', ')    print (p.__ CLASS__)

Run results

The person class for the current program

    • __module__

Used to represent the module in which the current action object resides

#-*-Coding:utf-8-*-__author__ = "MuT6 Sch01ar" class Person (object): "    This class is used to describe the"    def __init__ (self,name , age):        self.name = name        self.age =ageif __name__ = = ' __main__ ':    p = person (' John ', ')    print (p.__module __)

Run results

    • __call__

The instantiated object is followed by parentheses to execute the method

#-*-Coding:utf-8-*-__author__ = "MuT6 Sch01ar" class Person (object): "    This class is used to describe the"    def __init__ (self,name , age):        self.name = name        self.age =age    def __call__ (self, *args, **kwargs):        print ("Test for Class") if __name__ = = ' __main__ ':    p = person (' John ', ')    p ()

Run results

If the method is not defined, the object that is instantiated directly calls an error

    • __str__

When you print an object, the return value of the method is printed

#-*-Coding:utf-8-*-__author__ = "MuT6 Sch01ar" class Person (object): "    This class is used to describe the"    def __init__ (self,name , age):        self.name = name        self.age =ageif __name__ = = ' __main__ ':    p = person (' John ', ')    print (p)

Run

If the __str__ method is not defined, the object's memory address will be returned if it is printed directly.

If there is a __str__ method defined.

#-*-Coding:utf-8-*-__author__ = "MuT6 Sch01ar" class Person (object): "    This class is used to describe the"    def __init__ (self,name , age):        self.name = name        self.age =age    def __str__ (self):        return "The person Class" if __name__ = = ' __ main__ ':    p = person (' John ', ')    print (p)

Run

Instead of the memory address of the object, the return value of the __str__ method is printed

    • __dict__

Used to view all members of a class or object, expressed in a dictionary format

#-*-Coding:utf-8-*-__author__ = "MuT6 Sch01ar" class Person (object): "    This class is used to describe the"    def __init__ (self,name , age):        self.name = name        self.age =age    def __str__ (self):        return "The person Class" if __name__ = = ' __main __ ':    p = person (' John ', ')    print (person.__dict__)    print (p.__dict__)

Run results

Python Class (vii)-Special member Method of class

Related Article

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.