Python's dynamic instance properties for missing traps

Source: Internet
Author: User

--Where to see, where to think, where to remember

Many times, many people learn python, will ignore a lot of things, most of them are staring at can "shipping" on the line, but usually in the reading of other people's code when found, can not understand ... On the one hand, their own code skills and experience is not enough, on the other hand, the things they master are not comprehensive, and these are often the basis of things, not a big thing.

First: Dynamic instance properties

Python class, Object-oriented things and other languages are not the same, such as the properties of the instance can be dynamically assigned, originally did not, you can add yourself, even if the class is not defined, it can be used, very convenient, but it is recommended to define a row of complete best, the following a particle:

>>> class Address (object): ...         def __init__ (self,name,phone): ...               Self.name = Name               ... Self.phone = phone ... >>> jhon = Address (' Jhon Dov ', ' 123456 ') >>> jhon.name ' Jhon Dov ' >>> Jhon.phone ' 123456 ' >>> jhon.wa = ' aaa ' >>> print jhon.waaaa
Jhon.wa This is the dynamic allocation of the instance attribute, the WA attribute is not defined in the original class, but we can create one by Jhon instance.

Second, variable immutable, transmitted by reference value

Whether to pass the value or to pass the reference. Sometimes it's messy, because it's not very understanding, but it's very well-differentiated in Python, for example, a particle:

>>> List1 = [1, ' A ', [' foo ', ' Bar '], (' CC ', ' Opo ')]>>> list2 = list1>>> list2[0]1>>> list2[2][' foo ', ' Bar ']>>> list2[3] (' cc ', ' Opo ') >>> list2[3][0] ' cc ' >>> list2[0] = 2>> > list2[2][0] = ' Wang ' >>> list1[2, ' a ', [' Wang ', ' Bar '], (' CC ', ' Opo ')]>>> list2[2, ' a ', [' Wang ', ' Ba R '], (' CC ', ' Opo ')]>>> list1[3][0] = ' dd ' Traceback (most recent call last):  File "<stdin>", line 1, in <module>typeerror: ' Tuple ' object does not support item assignment
It can be seen that the direct assignment of this pass is a reference, that is, List1, List2 point to the same place in memory, like a person took two names, this is still very good understanding

One more chestnut:

>>> alist = List (list1) >>> alist[2, ' a ', [' Wang ', ' Bar '], (' CC ', ' Opo ')]>>> list1[2, ' a ', [' Wan  G ', ' Bar '], (' CC ', ' Opo ')]>>> alist[0] = 3>>> alist[1] = ' Z ' >>> alist[2][1] = "Rab" >>> Alist[3][0] = "111" Traceback (most recent call last):  File "<stdin>", line 1, in <module>typeerror: ' TUPL  E ' object does not support item assignment>>> alist[3] = [' 1 ', ' 2 ', ' 3 ']>>> alist[3, ' Z ', [' Wang ', ' Rab '], [' 1 ', ' 2 ', ' 3 '] >>> list1[2, ' a ', [' Wang ', ' Rab '], (' CC ', ' Opo ')]
This time is not directly assigned, but at the beginning we compare alist and List1, found the same content, look down, when the first two elements in the alist changes, the final and List1 not the same, list1 the first two elements have not changed, Alist changed, this is because the first two elements, One is the integer one is the string, is immutable, so, the first two elements of the alist is actually list1 the first two elements of the value of the effect, that is, the two are only the same value, two in memory, but the position in memory is not the same, so change the alist in the first two elements, Does not affect the list1; then look at the [' Wang ', ' Bar '] in List1, which is itself a list, we know that the list is mutable, so in Python, this list is a reference, only one copy in memory, so, change the list in Alist, List1 also change, finally demonstrated that the tuple in the list, the tuple itself is not mutable, so change the contents of the tuple will error, and pass the value, but this tuple as a list of elements, we can replace the whole.

May say the more disorderly, oneself try to know, this is a "shallow copy" and "Deep copy" problem, shallow copy as the name implies, copy is very shallow, can be understood as scalping hands of the guy, drink a drink, is a quote, and then the specific point of the chestnut is, borrow someone else, no matter how borrowed, something is a, Do not increase themselves, if someone to borrow something bad, it is really bad; deep copy, value, everyone a drink, a chestnut is, you see others a cup is very good-looking, you want to have one, so you also bought a cup, two cups is the same, but the owner is not the same, If you accidentally spit the glass, other people's cups can not be followed by bad ...

adjourned

Python's dynamic instance properties for missing traps

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.