A preliminary understanding of the reference and class attributes of the zero-learning python

Source: Internet
Author: User
Python is a high-level programming language for interpreting, object-oriented, and dynamic data types. Since the the early 1990s Python language was born, it has been widely used in processing system management tasks and web programming. Python has become one of the most popular programming languages. In January 2011, it was ranked as the 2010 language by the Tiobe programming language leaderboard. Since 2004, Python usage has grown linearly.

Python is designed with a clear, consistent style, which makes Python an easy-to-read, easy-to-maintain, widely-used language that is popular with many users.

In view of the above advantages, could not help Python to learn a little bit of harvest, share to everyone.

The recent object reference mechanism for Python has been slightly researched, leaving notes for review.

The first one is clear: all objects in "python.

So, what does this mean?

The following code:

Copy the Code code as follows:


#!/usr/bin/env python

A = [0, 1, 2] # a simple list

# Initially, the ID of the list and the individual elements is like this.
Print ' origin '
Print ID (a), a
For X in a:
Print ID (x), X
print '----------------------'

# We change the first element
print ' After change a[0] '
A[0] = 4
Print ID (a), a
For X in a:
Print ID (x), X
print '----------------------'

# We're going to change the second element
print ' After change a[1] '
A[1] = 5
Print ID (a), a
For X in a:
Print ID (x), X
print '----------------------'

# Look back and write a 0, what's the ID?
print ' How to about const 0? '
Print ID (0), 0

The results of the operation are as follows:

Copy the Code code as follows:


Pastgiftmacbookpro:python pastgift$./reftest.py
Origin
[0, 1, 2]
0
1
2
----------------------
After change a[0]
[4, 1, 2]
4
1
2
----------------------
After change a[1]
[4, 5, 2]
4
5
2
----------------------
How about the const 0?
0

From the "origin" section, the address of each element in the list is exactly 24, pointing to the respective data-which reminds me of the array.

After modifying the value of a[0], it is found that the address of a[0] has changed. In other words, the assignment statement actually just makes A[0] point back to the other object. In addition, note that the address of a[0] and the address of a[2] differ by 48 (2 24).

When the a[1] is modified again, the address of a[1] also changes, interestingly, this a[1] address and a[0] address is 24, and the original a[2] Difference 72 (3 24).

Finally, when the address of the number 0 is printed directly, it is found to be exactly the same as the address of the first a[0].

At this point, it is basically possible to say that even the elements in the list are actually references. Modifying the elements in the list is actually modifying the reference.

For class properties in Python, it has been mentioned that "class properties are shared between the same class and its subclasses, and modifying the class properties affects all objects of the same class and its subclasses."

Sounds scary, but after careful study, it is not a big deal.

The following code:

Copy the Code code as follows:


#!/usr/bin/env python

Class Bird (object):
name = ' Bird '
Talent = [' Fly ']

Class Chicken (Bird):
Pass

Bird = Bird ();
Bird2 = Bird (); # Similar Instances
Chicken = chicken (); # Sub-class instances

# It's the beginning.
print ' Original attr '
Print ID (bird.name), Bird.name
Print ID (bird.talent), bird.talent
Print ID (bird2.name), Bird2.name
Print ID (bird2.talent), bird2.talent
Print ID (chicken.name), Chicken.name
Print ID (chicken.talent), chicken.talent
print '----------------------------'

# Let's change the name.
Bird.name = ' Bird name changed! '

print ' After changing name '
Print ID (bird.name), Bird.name
Print ID (bird.talent), bird.talent
Print ID (bird2.name), Bird2.name
Print ID (bird2.talent), bird2.talent
Print ID (chicken.name), Chicken.name
Print ID (chicken.talent), chicken.talent
print '----------------------------'

# try to be a genius (modify the elements in the class properties)
Bird.talent[0] = ' walk '

print ' After changing talent (a list) '
Print ID (bird.name), Bird.name
Print ID (bird.talent), bird.talent
Print ID (bird2.name), Bird2.name
Print ID (bird2.talent), bird2.talent
Print ID (chicken.name), Chicken.name
Print ID (chicken.talent), chicken.talent
print '----------------------------'

# change to a new talent tree (entire class property replaced)
Bird.talent = [' Swim ']

print ' after reassign talent '
Print ID (bird.name), Bird.name
Print ID (bird.talent), bird.talent
Print ID (bird2.name), Bird2.name
Print ID (bird2.talent), bird2.talent
Print ID (chicken.name), Chicken.name
Print ID (chicken.talent), chicken.talent
print '----------------------------'

# Wash off the new talent tree (modify the elements in the new class property)
Bird.talent[0] = ' Dance '

print ' changing element after reassigning talent '
Print ID (bird.name), Bird.name
Print ID (bird.talent), bird.talent
Print ID (bird2.name), Bird2.name
Print ID (bird2.talent), bird2.talent
Print ID (chicken.name), Chicken.name
Print ID (chicken.talent), chicken.talent
print '----------------------------'

Operation Result:

Copy the Code code as follows:


Pastgiftmacbookpro:python pastgift$./changeattributetest.py
Original attr
Bird
[' Fly ']
Bird
[' Fly ']
Bird
[' Fly ']
----------------------------
After changing name
Bird name changed!
[' Fly ']
Bird
[' Fly ']
Bird
[' Fly ']
----------------------------
After changing talent (a list)
Bird name changed!
[' Walk ']
Bird
[' Walk ']
Bird
[' Walk ']
----------------------------
After reassign talent
Bird name changed!
[' Swim ']
Bird
[' Walk ']
Bird
[' Walk ']
----------------------------
changing element after reassigning talent
Bird name changed!
[' Dance ']
Bird
[' Walk ']
Bird
[' Walk ']
----------------------------

At the time of "origin", the same type of object, the same class attribute of the subclass object, is the same address-this is called "sharing".

After modifying name, only the modified Object Name property has changed. This is because the assignment to name is actually a string that is re-referenced. The string itself has not changed. So there is no interaction between the same class and the subclass.

Next, modify the elements in the talent. At this point, the situation changed: the talent properties of the same class and its subclasses all changed together-it's good to understand because they all refer to the same memory address, which refers to the same object.

Next, re-assign the value to talent, which is changed to refer to another object. As a result, only the talent property of this instance has changed. As you can see from the memory address, the talent property of this instance and other instances no longer points to the same object. That is to say, "this instance is already out of the circle".

Then, after the final modification of the elements in the talent, the result of no effect on the other instances is well understood. Because already is "outside the circle person", I again how to toss also is own matter.

Therefore, the "class attribute affects each other in the same category and its subclasses" must have a precondition: After the instance is established, its Class property has never been re-assigned, that is, the class attribute still points to the memory address that was originally pointed to.

Last mention of object properties

The following code:

Copy the Code code as follows:


#!/usr/bin/env python

Class Bird (object):
def __init__ (self):
Self.talent = [' Fly ']

Bird = Bird ()
Bird2 = Bird ()

# The first situation
print ' Origin '
Print ID (bird.talent), bird.talent
Print ID (bird2.talent), bird2.talent
print '--------------------'

# Modify the properties of one of the objects
Bird.talent[0] = ' walk '

print ' After changing attribute '
Print ID (bird.talent), bird.talent
Print ID (bird2.talent), bird2.talent
print '--------------------'

# dead: Two object's properties point to the same memory address, and then modify
Bird.talent = Bird2.talent
Bird.talent[0] = ' swim '

print ' Assign to another attribute and change it '
Print ID (bird.talent), bird.talent
Print ID (bird2.talent), bird2.talent
print '--------------------'

Operation Result:

Copy the Code code as follows:


Pastgiftmacbookpro:python pastgift$./changeattributetest2.py
Origin
[' Fly ']
[' Fly ']
--------------------
After changing attribute
[' Walk ']
[' Fly ']
--------------------
Assign to another attribute and change it
[' Swim ']
[' Swim ']
--------------------

Because object properties are exactly the same as the content (the properties are generally the same after the initial initialization), they are also assigned to a completely different memory address. Therefore, there is no case of "influence between homogeneous objects".

But if you have one object's properties and another object's properties pointing to the same address, the two (but only between them) are implicated.

  • 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.