About Python "Assignment is a reference to creating an object", what do you think? What does python mean when everything is an object?

Source: Internet
Author: User
Why do I feel so convenient ah ...
For example, the following code:

x = [1,2,3,4] y = x y [0] = 4 Print x >> x = [4234]

Reply content:

Very convenient.
Object.h


And everything in Python comes from this, and these two macros are defined as:
actually can find pyobject_var_head also just pyobject_head add a ob_size, so Python, each object has the same object head, so we just need to use a pyobject * You can refer to an arbitrary object, regardless of what object it is actually, so when there is a Python object in memory, the meaning of the first few bytes of the object will certainly match our expectations, that is, Pyobject_head. The _pyobject_head_extra in the Pyobject_head macro definition is actually just a doubly linked list of _object, Actually can find Pyobject_var_head also just pyobject_head plus a ob_size, so Python, each object has the same object head, so we just need to use a pyobject * can refer to any object, Regardless of what the object actually is, so when there is a Python object in memory, the meaning of the first few bytes of the object will certainly match our expectations, that is, Pyobject_head. The _pyobject_head_extra in the Pyobject_head macro definition is actually just a doubly linked list of _object,
while OB_REFCNT is a counter for reference counting, struct _typeobject *obtype is a type that represents the type object of the object type: While OB_REFCNT is a counter for reference counting, struct _typeobject *obtype is a type that represents the type object of the object type:

By Pyobject_var_head you can discover that a type is actually an object (everything is an object), and the type object is of the type associated with the type object: Pytype_type:
and even if the simple int is to fill the things here: And even if the simple int is to fill the things here:
so, even int is an object So, even int is an object
So even if you use
i = 47
j = 47
is also a reference to 47, and

By ID, you can see where the memory reference is the same, and why? Because to reduce the overhead of frequent calls, the small int object pool technique is used:
so as long as more than 257 (including 257), then changed: So as long as more than 257 (including 257), then changed:
So, although M,n is a reference to 258 objects, it is a different memory address. So, although M,n is a reference to 258 objects, it is a different memory address.

The rest of you can continue to follow this route to explore the Python mechanism, and your question has been basically answered, when you doubt some problems, the source code is the best explanation. :-)

p.s. Python version: 2.7.8 variables are generally available in three different styles:
Reference: A variable is the name of an object that can be bound to any object at run time, such as Python.
Value: The variable is the name of the storage location, the binding is completed at compile time, the runtime can not be changed, such as C.
Mixed: Some types are value-typed, some are quoted, such as C # 's struct and class.
C + + is a special, basically can be said to be value-style, but because of the existence of aliases, so there are some features of the citation.

The difference between a reference and a value is that in an assignment operation, the assignment of a reference is the re-binding of the variable name, and the value assignment is the copy of the object.

They can simulate each other. Reference to the value of the assignment, only need explicit copy or copy-on-write on it. And the value of the want to have a reference to the assignment only need to use a pointer.

You're probably just getting used to the style of value-added-C + + and not adapting to a quoted style.

It may seem that the style is uniform and better. But in practice I think C # is the best mix. In fact, most of the languages are like this: objects are provided to programmers in a reference manner, and objects are assigned only to multiple variables pointing to the same object.
It's probably only a copy of the C + + assignment.
A = 258
In Python's view:
Creates a Pyintobject object with a value of 258;a that is a pointer to Pyobject, which points a to this Pyintobject object
The reason why you can do this is to explain the answer to @ Blue, and all the pyobject have the same head. Some objects in Python can be changed in place (that is, mutable objects), which include lists, dictionaries, collections, and some custom objects. For immutable objects, such as integers and strings, there is no problem with the main question. Like what:
>>>a=123>>>b=a>>>a=321>>>printa,b321123
The point is that when you write down y=x, you actually do not create a new Y, but a mechanism similar to that quoted in C + +, which you can look at:

>>> x = [1,2,3,4]
>>> y = x
>>> ID (x)
43246536L
>>> ID (y)
43246536L

So x and y are actually a thing.

If you want to copy a list, you need to use the
>>> y = list (x)

Or

>>> y = x[:] You add "pointers" together to understand. You want a deep copy to use y=x[:].

In fact, there is nothing bad, the object of the py is divided into variable and immutable two, as long as the understanding of this is clear. ID ()
  • 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.