zoukankan      html  css  js  c++  java
  • Arguments pass reference or value ? Mutability

    Mutability asks the question of whether an object enables the modification of its value. All Python objects have the following three attributes: type, id, and value.Type and value should hopefully be obvious; the “id” is simply an identification number that uniquely identifies an object from all other objects running in the interpreter.
    All three attributes are almost always read-only, meaning they cannot be changed during the lifetime of an object.The only possible exception is the value: If the value can be changed, then it is a mutable object; otherwise it is immutable. Simple or “scalar” types, including integers and other numeric types, are immutable as are string types like str and unicode, and finally tuples. Just about everything else—lists, dicts, classes, class instances, and so forth—is mutable.
    One area of caution with mutable objects is in calling their methods. If the method you call modifies a mutable object in any way, then it typically does so “in place,” meaning the data structure is modified directly instead of returning a modified copy of the object (in which case the function returns None instead).
    mylist = [1, 'a', ['foo', 'bar']]
    mylist2 = list(mylist)
    mylist2[0] = 2
    mylist[2][0] = 'biz'

    then the list 'mylist2' is a copy of the list mylist, but you must know that the list() is a function too. Then the inner list of the list mylist is copyed by reference to the new list mylist2.

  • 相关阅读:
    监督学习——AdaBoost元算法提高分类性能
    监督学习——logistic进行二分类(python)
    监督学习——朴素贝叶斯分类理论与实践
    nrm 工具的使用
    Linux下安装MySQL
    Node环境配置
    07.移动端类库
    06.网页布局
    05.预处理器
    04.触屏事件
  • 原文地址:https://www.cnblogs.com/henyihanwobushi/p/2676962.html
Copyright © 2011-2022 走看看