The difference between go and python slices

Source: Internet
Author: User

Go has a sliced slice type,python has lists and tuples, both of which have slice operations.

But their slicing operations are completely different.

First of all, the first, go slices, whose members are of the same type, Python's list and tuples do not restrict the type.

Both languages have [a:b] this slice operation, the meaning is similar, but go of a, b two parameters can not be negative, python can be negative, this is equivalent to from the end of the forward number.

two languages have [a:b:c] this slice operation, the meaning is completely different. Go's C, which represents the capacity, while Python's C represents the step size.

But the biggest difference is:

A python slice produces a new object, the operation of a member of the new object does not affect the old object, and the go slice produces a reference to a part of the old object, and the action on its members affects the old object.

The reason, or the bottom-level implementation of the difference.

Go slices, the bottom layer is a ternary group, a pointer, a length, a capacity. The pointer points to a contiguous amount of memory, the length is the number of members, and the capacity is the maximum number of members. When slicing, it is not generally necessary to apply for new memory, but to move the original pointer, and then return with the new length and capacity to form a slice type value. In other words, the slice operation of go usually uses memory with the tiles that generate the slice.

Not only the slices, but also the slices of the string and array are the same, and usually the memory is shared.

Of course, there are exceptions, that is, when slicing, the capacity provided is too large, this time the new memory will be requested and copied, or the slice append out of capacity, this is the case. At this point, the new slice will not share memory with the old slice. (Panic if you provide less than the length when slicing/creating)

A list of Python, in fact, is an array of pointers. Of course, there will be some vacancies on the lower level, but the basic is the number of groups. Slicing them will create a new array, note that is to create a new array! the list of Python can have no capacity concept.

This, in fact, also embodies the difference between scripting language and compiler language. Although both languages have similar slicing operations, Python's main goal is convenience; The go main goal is to quickly (and compensate for the defect in the drop pointer operation).


The difference between go and python slices

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.