What is the difference between doing

class a:

def __init__(self):

self.val=1

to doing

class a:

val=1

def __init__(self):

pass

解决方案

class a:

def __init__(self):

self.val=1

this creates a class (in Py2, a cruddy, legacy, old-style, don't do that! class; in Py3, the nasty old legacy classes have finally gone away so this would be a class of the one and only kind -- the **good* kind, which requires class a(object): in Py2) such that each instance starts out with its own reference to the integer object 1.

class a:

val=1

def __init__(self):

pass

this creates a class (of the same kind) which itself has a reference to the integer object 1 (its instances start out with no per-instance reference).

For immutables like int values, it's hard to see a practical difference. For example, in either case, if you later do self.val = 2 on one instance of a, this will make an instance reference (the existing answer is badly wrong in this respect).

The distinction is important for mutable objects, because they have mutator methods, so it's pretty crucial to know if a certain list is unique per-instance or shared among all instances. But for immutable objects, since you can never change the object itself but only assign (e.g. to self.val, which will always make a per-instance reference), it's pretty minor.

Just about the only relevant difference for immutables: if you later assign a.val = 3, in the first case this will affect what's seen as self.val by each instance (except for instances that had their own self.val assigned to, or equivalent actions); in the second case, it will not affect what's seen as self.val by any instance (except for instances for which you had performed del self.val or equivalent actions).

python类定义中__init__(),在__init__中定义一个成员以在python中的类体中定义它的区别?...相关推荐

  1. python变量值发生变化时输出语句_有一个字符串变量s = 'python program',语句print(s[1:5:2]) 的输出结果是:_学小易找答案...

    [判断题]print('936'.isdigit()) 的输出结果是 True [单选题]下面关于"义务论"伦理学和"后果论"伦理学的比较描述错误的一项是?() ...

  2. python 案例串接_来撸串,一个案例轻松认识Python 字符串——翻转拼接游戏

    键盘输入一个字符串和数字,从头部或尾部截取指定数量的字符串,然后将其翻转拼接.将字符串在第n个字符处翻转拼接,例如输入,python,2;输出头部翻转:thonpy;尾部翻转:onpyth. 字符串是 ...

  3. python爬取公众号阅读量_分享一个牛逼的Python项目:公众号文章爬虫

    我订阅了近 100 个公众号,有时候想再找之前读过的文章,发现搜索起来特别困难,如果忘了收藏,估计得找半小时,更让人无语的是,文章已经发布者删除,或者文章因违规被删除.那么有没有这样的爬虫,可以将公众 ...

  4. python中文转拼音不用第三方库_又一个奇葩要求,Python是如何将“中文”转“拼音”的?...

    作者:黄同学 这年头什么样子的需求都会出现,下面这张图就是很好的体现了. 这就是说为啥要你学学Python啦!保不准你的领导会有各种奇葩需求,对于像Python这样的"万金油"编程 ...

  5. python怎么打印网页_教你分分钟开发一个属于自己的python模块(一)——能够直接在浏览器打印的方法...

    曾经,用惯了python print命令的人,惊叹于python语法的精简:后来,用过了tornado.django等web开发框架,不得不佩服当初开发这些框架的人们.于是,我们开始使用它们的框架== ...

  6. python 结构体数组 定义_一篇文章弄懂Python中所有数组数据类型

    前言 数组类型是各种编程语言中基本的数组结构了,本文来盘点下Python中各种"数组"类型的实现. list tuple array.array str bytes bytearr ...

  7. C语言高级编程:结构体中定义数组的“妙用”

    1. 说明 1)相同类型结构体变量可以直接赋值 2)在结构体中定义一个数组,可以用赋值语句拷贝整个数组,这也是数组间赋值的一个间接实现方式 3)在结构体中定义一个数组,可以传值调用的方法把它传递给函数 ...

  8. c++结构体总结(结构体定义,结构体数组,结构体指针,结构体嵌套结构体,结构体做函数参数,结构体中 const使用场景)

    看完b站黑马程序员之后的借鉴和笔记 1.什么是结构体,有什么作用? 在C/C++中,结构体是用户定义的数据类型.它可以把几种不同类型的数据项集合成结构体这样一个单一类型. 2. 结构体定义和使用 #i ...

  9. 【Java基础】Java中的char是否可以存储一个中文字符之理解字符字节以及编码集

    Java中的一个char采用的是Unicode编码集,占用两个字节,而一个中文字符也是两个字节,因此Java中的char是可以表示一个中文字符的. 但是在C/C++中由于采用的字符编码集是ASCII, ...

最新文章

  1. 后MATLAB时代的七种开源替代,一种堪称完美!
  2. Hibernate配置(2)
  3. PHP100精华:PHP打印函数集合
  4. 【python】队列——用链表实现队列操作
  5. boost::mp11::mp_join相关用法的测试程序
  6. 教你制作U盘,内存卡,硬盘等等等的漂亮背景
  7. 【转】增量式PID控制算法
  8. ZABBIX API简介及使用
  9. css布局难,运用 CSS布局到底有多难?[多图]
  10. latex常用的公式
  11. Linux中Apache服务器的简单配置
  12. ubuntu 14.04英文环境设置成中文
  13. 274-MySQL(查看源码的工具sourceinsight)
  14. csv文件超过104万数据怎么办
  15. CloudSim仿真流程研究(一)
  16. 骨骼的动作识别数据集_基于关节数据的人体动作识别
  17. npm ERR! Error: tunneling socket could not be established的解决问题
  18. python分析乐谱和音乐的包music21
  19. nvm-windows下载与安装
  20. 【无标题】黑炫酷的监控界面,实际上是用了什么开源工具?

热门文章

  1. dj电商-项目开发流程
  2. 新手教程——在Linux Mint 16中找到保存的WiFi密码
  3. iOS:Covert p12 back to CSR
  4. UIScrollView相关问题(计算分页)
  5. Windows Server 2012 R2安装体验
  6. 如何跳过或去除“非正版Windows系统“信息
  7. 探索HTTP传输中gzip压缩的秘密
  8. JS事件循环机制:同步与异步任务 之 宏任务 微任务
  9. 面试精讲之面试考点及大厂真题 - 分布式专栏 15 如何解决消息重复,保证消息顺序问题
  10. CentOS 新增swap交换空间