https://blog.csdn.net/hsuxu/article/details/7785835

python mutable as default parameter(NONONO)

def f(l=[]):

l.append(l)

print(l)

f()

f()

那么在python那些是immutable呢?
numbers, strings, tuples, frozensets
其实,还有一种特殊情况,就是自定义的类型呢?
一般情况下,程序员自定义的python类型都是mutable的,但是如果你想定制immutable的数据类型,那么你必须重写object的__setattr__和__delattr__方法,如下:

  1. class Immutable(object):
  2. def __setattr__(self, *args):
  3. raise TypeError("can't modify the value of immutable instance")
  4. __delattr__ = __setattr__
  5. def __init__(self, value):
  6. super(Immutable, self).__setattr__("value", value)
--
刘林强 136-1133-1997
liulinqiang@unipus.cn
北京外研在线教育科技有限公司
外语教学与研究出版社

python immutable and mutable的更多相关文章

  1. python 可更改(mutable)与不可更改(immutable)对象

    在 python 中,strings, tuples, 和 numbers 是不可更改的对象,而 list,dict 等则是可以修改的对象. 不可变类型:变量赋值 a=5 后再赋值 a=10,这里实际 ...

  2. python的mutable变量与immutable变量

    python的变量分为mutable(可变的)和immutable类型. mutable:dict, list immutable:int , string , float ,tuple..

  3. python's mutable & immutable

    [python's mutable & immutable] python里面的类型其实也分为immutable和mutable二种,对于mutable,如果b指向a,当b改变时,a也会改变: ...

  4. Python 学习笔记(三)Function

    python引用变量的顺序: 当前作用域局部变量->外层作用域变量->当前模块中的全局变量->python内置变量 1. Scope: • If a variable is assi ...

  5. The internals of Python string interning

    JUNE 28TH, 2014Tweet This article describes how Python string interning works in CPython 2.7.7. A fe ...

  6. Python Tutorial 学习(九)--Classes

    ## 9. Classes 类 Compared with other programming languages, Python's class mechanism adds classes wit ...

  7. Python的类(class)

    python 3.6 官方文档  https://docs.python.org/3.6/index.html python 3.6 的类 https://docs.python.org/3.6/tu ...

  8. 论python中的函数参数的传递问题。

    python是完全面向对象的语言,在参数传递的过程不能使用值传递,引用传递的概念,而应该使用immutable和mutable.在java中,除了object,其实还有8种基本数据类型,才有了参数传递 ...

  9. python 之禅 import this

    dongweiming的博客 前言 我这个博客一直都是一些技术分享,show code的地方,我从来没有写过个人生活或者情感杂谈,当然我也从来没有谈论过我对什么东西的喜恶. 很多人喜欢喷XX语言,喜欢 ...

随机推荐

  1. CSA Enterprise Architecture图

    https://research.cloudsecurityalliance.org/tci/index.php/explore/

  2. Smack类库详细介绍

    原文地址:http://blog.csdn.net/xunshu/archive/2008/03/27/2223817.aspx Smack是一个为使用XMPP服务器聊天和发送即时消息交流而提供的库. ...

  3. centos7系统下安装php-fpm并配置nginx支持并开启网站gzip压缩

    注:此处不介绍nginx的安装.以下教程默认已安装nginx. 1. yum install -y php-fpm yum install php-pdo yum install php-mysql ...

  4. pixmap和label设置图片自适应大小

    在label中添加pixmap来显示图片时,当图片过大时图片显示不全. 1.这时可以使用pixmap的scared()方法,来设置图片缩放. QPixmap QPixmap.scaled (self, ...

  5. [Laravel] 02 - Route and MVC

    前言 一.良心资料 英文 Laravel 框架:https://laravel.com/ 教程:https://laracasts.com/series/ laravel-from-scratch-2 ...

  6. Apache Nginx URL 地址 重写

    URL重写这东西在工作中用了很多次了,但每次都忘记了要记得把知道的积累下来. 哎,要么认为没必要,要么就是没时间?! 一.Apache 篇 官方地址:http://man.chinaunix.net/ ...

  7. HTML 选择目录

    <input type="file" webkitdirectory directory multiple/>

  8. Linux内核 GPIO操作部分API

    内核中关于GPIO的操作API主要集中在<linux/of_gpio.h>和<linux/gpio.h>中,前者主要是GPIO直接与设备树相关的操作,在Linux 设备树操作A ...

  9. IntelliJ IDEA连接TFS local workspace无法正常签入

    前几天为了便于在本地修改,将TFS workspace的类型从Server修改为Local.基于Visual Studio的开发正常没有问题,用IntelliJ IDEA时却提示以下错误: Error ...

  10. Android数据解析——JSON

    示例一: 有这样一个JSON需要解析,比如: {"thresholds": {"1e-3": 65.3,"1e-5": 76.5," ...