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.

  • 相关阅读:
    最大期望算法 Expectation Maximization概念
    Apriori 关联算法学习
    mysql小问题
    C4.5决策树算法概念学习
    线性回归概念学习
    决策树概念学习
    Flink on Yarn运行机制
    Flink单机版安装与wordCount
    Kmeans算法学习与SparkMlLib Kmeans算法尝试
    数据挖掘10大算法详细介绍
  • 原文地址:https://www.cnblogs.com/henyihanwobushi/p/2676962.html
Copyright © 2011-2022 走看看