Fork me on GitHub

Python 入门学习之 不可变字符串数据类型分析(五)

Python代码分析:

 

# 值类型:
#
# 包含:字符串、元组、数值,本身不允许被修改(元组是列表的只读版 括号代替中括号)
#
# 引用类型: 包含:列表、字典,本身允许修改,字典的key
a=3

b=a

a=4

print(a,b)


m="aaa"
x=m
print(id(m))  #打印内存地址
print(id(x))
m="bbb"  # 开辟了新内存地址
print(id(m))
print(id(x))
print(m,x)

lista = [1,2]

listb = lista

lista[0] = 3

print(lista,listb)

 

 

C# 代码分析

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string a = "aaaaa";
            string b = a;

            unsafe
            {
                fixed (char* p = a)
                {
                    Console.WriteLine("a value:" + a);
                    Console.WriteLine("a address 0x{0:x} ",(int)p);
                    Console.ReadLine();
                }
                a = "a---change";
                fixed (char* p = b)
                {
                    Console.WriteLine("b value:"+b);
                    Console.WriteLine("b address 0x{0:x} ", (int)p);
                    Console.ReadLine();
                }

                fixed (char* p = a)
                {
                    Console.WriteLine("a value:" + a);
                    Console.WriteLine("a address 0x{0:x} ", (int)p);
                    Console.ReadLine();
                }
            }
        }
    }
}

 

 结论: Python 和c#一样   字符串类型 都是开辟了新内存 地址  ,不会改变原先的引用地址和值。

posted @ 2017-11-16 13:13  低调的神  阅读(118)  评论(0编辑  收藏  举报