variable python-12-

1, variable references

  Variables and data are stored in memory

  In python parameter transfer function, and return values are by reference passed

  The concept of reference :

    Variables and data are separately stored

    Data stored in a memory location

    Variable holds the address data in memory

    Variable data recorded in an address called reference

    Using the id () function can view the memory addresses where the stored data

  Note: If the variable is defined, when assign values ​​to variables, in essence, is to modify the reference data,

    Variable is not a reference to previous data

    Variables to assign to the new reference data

 

  When you call the function, passing essentially save data references argument, not argument saved data

  

   The same address

  When there is a return value, but also a reference to the returned data

  

 

 

2, variable type and immutable

  Immutable type , the data memory can not be modified:

 

 

    Digital type int, bool, float, complex, long (2.x)

    String str

    Tuple tuple

  Variable type , the data memory can be modified:

    List list

    Dictionary dict

    Dictionary key can only be immutable data types

    

 

 3. hash hash

  python built into a function called hash (o) of

    Receiving a immutable data types as a parameter

    The result is an integer

  It is a hash algorithm, and its role is to extract the data signature (like a fingerprint)

    The same content have the same result

    Different content to get different results

  In python, when setting up the dictionary of key-value pairs, will first hash the key has to decide how to save the data dictionary in memory, in order to facilitate the subsequent operation of the dictionary: add, delete, change, search

    键值对的key必须是不可变类型的数据

    键值对的value可以是任何类型的数据

 

4、局部变量和全局变量

  局部变量,是在函数内部定义的变量,只能在函数内部使用

  全局变量,是在函数外部定义的变量(没有定义在某一个函数内),所有函数内部都可以使用这个变量

  局部变量:

   是在函数内部定义的变量,只能在函数内部使用

   函数执行结束后,函数内部的局部变量,会被系统回收 

   不同的函数,可以定义相同名字的局部变量,不会互相影响

   生命周期

    生命周期 就是变量从被创建 到被系统回收的过程

    局部变量 在函数执行时才会被创建

    函数执行结束后 局部变量被系统回收

    局部变量在生命周期内,可以用来存储 函数内部临时使用到的数据

  全局变量:

   函数内部不允许直接修改全局变量的值

   如果希望修改全局变量的值,需要global修饰

   为保证所有函数都用到全局变量,应该将全局变量定义在其他函数的上方

   

 

 

 

Guess you like

Origin www.cnblogs.com/onroad2019/p/11990588.html
Recommended