Python simply talks about "class"

Source: Internet
Author: User

Article Source: http://www.cnblogs.com/winstic/, please keep this connection

Object-oriented is a feature of Python language, and class is the core of object-oriented programming.

Let's start with a declaration about a class:

classMyClass (object):#Inherit Object        """It ' s my first class defined"""    #Document StringVersion = 1.0#Static MembersTestlist = [1, 2, 3]        def __init__(Self, NM ="John"):            """Constructor"""Self.name=nmdefShowName (self):"""Display Name"""            Print "your name is:", Self.namedefshowversion (self):"""Display version"""            Print "The version is:", self.version

Static variables declared in a class (also called Class properties) version, Testlist will be shared by all instances and methods of the class show*

Another obvious feature in the definition of a class is that each function has a parameter of self; We can think of the this pointer in C + +, yes, they have a similar function, in Python, Self is a reference to a class instance , remember that the reference to the class instance is not the class itself, then I want to refer to the actual class, of course, no problem, you can use self.__ Class__

__init__ ()

This function is very special, we know that there is a double underscore in python in the special method, then he has what is special, can quickly associate with the constructor (note the difference OH)
This is often seen in classes, and can be used as constructors, but unlike other language constructors, which do not create object instances , just the first method that was executed after the object was created, the main purpose is to accomplish some necessary initialization work; There will always be a system default of __init__ (), do nothing, we define variables in the __init () __ method, but this variableexists only in the class instance and is not part of the actual class itself

Class instance modifies a property value?

Each class instance can only modify its own property values, which may be said to be unrelated in multiple instances of the class, but there is a precondition: The property value must be an immutable object (why?). Please continue to see ...)

  

  While you can change your own property values by object, when you do this, polish your burner ginkin to see if the Class property is mutable or immutable, but you can use the class name if you want to change the class property . The attribute name is =xxxx , and the following will be a simple analysis of the difference between an immutable object and a Mutable object in the case of an attribute value change:

# for immutable object (number) operations, the instances do not affect each other >>> test1 = myClass ()>>> test2 = myClass ()> >> test1.version = 2.0>>>is:  2.0>>>are:  1.0
# Synchronize updates between instances when you operate on a Mutable object (list) >>> test1.testlist[0] = 10>>> test1.testlist[10, 2, 3 ]>>> test2.testlist[10, 2, 3]
    • Cause Analysis:

    When you change a class property, if the property is immutable, the property is copied out of the __dict__ of the current object, and the value of the Original class itself does not change.

>>> test1.__dict__{'version': 2.0,'name':'John'}>>> test1.__class__. Version1.0>>> test1.__class__.__dict__Dict_proxy ({'__module__':'__main__', 'version': 1.0,'__init__': <function__init__At 0x025ffab0>'__dict__': <attribute'__dict__'Of'MyClass'Objects>,'__weakref__': <attribute'__weakref__'Of'MyClass'Objects>,'showversion': <function showversion at 0x025ffb30>'testlist': [10, 2, 3],'__doc__':"It ' s my first class defined",'ShowName': <function showname at 0x025ffaf0>})

If the Class property value is mutable (mutable), because it is still operating in the original memory address, it does not produce the corresponding copy, and once the value changes, it changes the corresponding attribute value in the original class, which in turn affects the other instances of the class

>>> test1.testlist[, 2, 3]>>> test1. __class__ . testlist[, 2, 3]>>> test2.testlist[10, 2, 3]

All when we change class property values through class instances, pay special attention to seeing if the Class Property object is mutable (mutable/immutable)to avoid unnecessary errors.

Python simply talks about "class"

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.