Python Basics Finishing Notes (vii)

Source: Internet
Author: User

One. Python's class attributes and instance properties are a point of attention

1 classTestatt ():2AAA = 103 4 defMain ():5     #Case 16Obj1 =Testatt ()7Obj2 =Testatt ()8     Printobj1.aaa, obj2.aaa, Testatt.aaa9 Ten     #Case 2 OneOBJ1.AAA + = 1 A     Printobj1.aaa, obj2.aaa, Testatt.aaa -  -     #Case 3 theTESTATT.AAA + = 2 -     Printobj1.aaa, obj2.aaa, Testatt.aaa -  - if __name__=='__main__': +Main ()

In a sample code, a class Testatt only one class object, and then by changing the properties of its different objects and the properties of the class, the result is as follows:

The change seems to be very special, the reasons for analysis are as follows:

The property acquisition mechanism in Python is a little bit from the underlying object to the top, and the first time you get OBJ1.AAA, OBJ2.AAA, because this class does not initialize the properties of the instance, so in fact obj1 and obj2 do not have the AAA attribute, so essentially obj1.aaa, obj2.aaa are testatt.aaa.

and for case2,obj1.aaa + = 1 This operation is actually to give Obj1 this instance created AAA attribute (with the value of TESTATT.AAA plus 1), so from the Case2 view its value is changed individually, and in CASE3 also no longer affected.

Now I'm going to change the code to run the results below and print out the __dict__ for each instance:

1 defMain ():2     #Case 13Obj1 =Testatt ()4Obj2 =Testatt ()5     Printobj1.aaa, obj2.aaa, Testatt.aaa6     PrintObj1.__dict__7     PrintObj2.__dict__8 9     #Case 2TenOBJ1.AAA + = 1 One     Printobj1.aaa, obj2.aaa, Testatt.aaa A     PrintObj1.__dict__ -     PrintObj2.__dict__ -  the     #Case 3 -TESTATT.AAA + = 2 -     Printobj1.aaa, obj2.aaa, Testatt.aaa -     PrintObj1.__dict__ +     PrintObj2.__dict__

In this way, it is obvious that the above situation can be proved.

Two. Python multiple inheritance note points:

In python2.7 there are classical and new classes, the former defined without the explicit inheritance, the latter will inherit the object class.

When searching for attributes after multiple inheritance, there is an obvious difference between the behaviors of the two:

The classic class is a depth-first search-that searches through the inheritance chain, then to the next priority inheritance chain, and so on.

The form class is a breadth-first search-a priority level search among inheritors of the same generation, a generation that cannot be searched, and then looked for in the higher fathers, one analogy.

The sample code is as follows:

1 classA (object):2     deffoo (self):3         Print "class A" 4         5 classA1 ():6     deffoo (self):7         Print "class A1"  8         9 classC (A):Ten     Pass One      A classC1 (A1): -     Pass -      the classD (A): -     deffoo (self): -         Print "class D"   -      + classD1 (A1): -     deffoo (self): +         Print "class D1"   A    at classE (C, D): -     Pass -      - classE1 (C1, D1): -     Pass -  inE =E () - E.foo () to    +e1 =E1 () -E1.foo ()

Here the number is the classic class, not with the new class, and then we run the result:

To prove the above statement.

But let's look at the performance in Python3, the same code results as follows:

So in the Python3, there is not the so-called classical class and the form of the difference between the class, and the sequence of multiple inheritance for the depth first.

Python Basics Finishing Notes (vii)

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.