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

Source: Internet
Author: User
Tags in python

Python is a high-level programming language for interpretive, object-oriented, Dynamic Data types. Since the the early 1990s Python language has been born, it has been widely used to deal with system management tasks and web programming. Python has become one of the most popular programming languages. January 2011, it was rated as the 2010 language by the Tiobe programming language rankings. Since 2004, Python usage has been growing linearly.

Python's design insists on a clear and uniform style, making Python a readable, maintainable, and widely used language that is popular with many users.

In view of the above various advantages, can not help to learn python, a little harvest, share to everyone.

The recent object-referencing mechanism for Python has been slightly studied, leaving notes for review.

First, one thing is clear: everything in "python is the object.

So what does this really mean?

The following code:

Copy Code code as follows:

#!/usr/bin/env python

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

# Initially, the list and the IDs of each of these elements are 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'll 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, just write a 0, id what?
print ' How about const 0? '
Print ID (0), 0

The results of the operation are as follows:

Copy Code code as follows:

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

From the "origin" section, the address of each element in the list is exactly 24, pointing to its own 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. That is, the assignment statement actually simply lets A[0] point back to another object. In addition, it is noted that the address of a[0] differs from that of a[2] by 48 (2 24).

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

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

At this point, the basic can be explained that even the list of elements, in fact, is also a reference. 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 class properties affects all objects of the same class and its subclasses".

It sounds scary, but after careful study, it's not really a big deal.

The following code:

Copy Code code as follows:

#!/usr/bin/env python

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

Class Chicken (Bird):
Pass

Bird = Bird ();
Bird2 = Bird (); # similar examples
Chicken = chicken (); # Subclass Instances

# in the beginning it's like this
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 '----------------------------'

# change a name and see
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 '----------------------------'

# a gift to try (modify the elements in the class attribute)
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 '----------------------------'

# To change a new talent tree (whole class attribute completely 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 away the new talent tree (modify the elements in the new class properties)
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 '----------------------------'

Run Result:

Copy 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 addresses of the same class attributes of the same object, subclass objects, are the same--this is called "sharing."

After you modify name, only the object Name property that is modified changes. This is because the assignment to name is actually a string that is being referenced again. The string itself has not changed. So there is no interaction between the same class and subclasses.

Next, modify the elements in the talent. At that point, things changed: The talent attributes of the same class and its subclasses all changed together-which is well understood because they all refer to the same memory address, referencing the same object.

Next, assign a value to the talent, which means to refer to another object. The result is that only the talent attribute of this instance has changed. As you can see from the memory address, the talent property of this instance and other instances is no longer pointing to the same object. It means "so far, this example is already outside the circle."

Then again, after modifying the elements in the talent, it is well understood that the results have no effect on other instances. Because it is already "outside the circle of people", I do not have to toss it is my own business.

Therefore, "class attributes interact with each other" must have a prerequisite: After the instance is established, its class attribute has never been assignable, i.e. the class attribute still points to the memory address originally pointed at.

Finally, mention object properties

The following code:

Copy Code code as follows:

#!/usr/bin/env python

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

Bird = Bird ()
Bird2 = Bird ()

# Just the beginning of the 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 '--------------------'

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

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

Run Result:

Copy Code code as follows:

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

Because the object property is exactly the same (the property content just initialized is generally the same), it is assigned to a completely different memory address. So there is no such thing as "influence between similar objects".

But if you let one object's properties and another object's properties point to the same address, the two (but only the other) 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.