Reference and copy of Python first-time experience objects

Source: Internet
Author: User

All those who have written about Java know that the reference of this object and object clone are completely unnecessary. As an object-oriented language, Python is not an example, most of his declaration or use of a variable is a reference to the object, but occasionally we will have to use the object clone, next, let's take an example to illustrate the differences between the object clone and object reference. This is very important. First, let's take a look at the code [python] ''' Created on 2013-1-25 @ author: Administrator ''' shoplist = ['apple', 'mango', 'carrot ', 'bana'] mylist = shoplist del shoplist [0] print ("shoplist is", shoplist) print ("mylist is", mylist) # copy mylist = shoplist [:] del shoplist [0] print ("shoplist is", shoplist) print ("mylist is", mylist) ''' Created on 2013-1-25 @ author: administrator ''' shoplist = ['apple', 'mango', 'carrot', 'bana Na '] mylist = shoplist del shoplist [0] print ("shoplist is", shoplist) print ("mylist is", mylist) # copy mylist = shoplist [:] del shoplist [0] print ("shoplist is", shoplist) print ("mylist is", mylist) Outputs [python] shoplist is ['mango ', 'carrot', 'bana'] mylist is ['mango', 'carrot', 'bana'] shoplist is ['carrot ', 'bana'] mylist is ['mango', 'carrot', 'Banana '] shoplist is ['mango', 'carrot', 'Banana '] Mylist is ['mango', 'carrot', 'bana'] shoplist is ['carrot', 'bana'] mylist is ['mango', 'carrot ', 'banana '] You can clearly understand the concept of reference to this sequence and object clone. You need to be familiar with clone of this sequence ., Shoplist [:] is not always used, but mark it

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.