博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python类内init外声明的属性与init内声明的对象属性的访问和操作区别
阅读量:5279 次
发布时间:2019-06-14

本文共 3261 字,大约阅读时间需要 10 分钟。

python类内init外声明的属性与init内声明的对象属性的访问和操作区别(面试题) 

 

 1.在ipython中输入以下代码,其输出会是什么?

In [1]: class ClassOut:   ...:     out_mem = 'out_mem'   ...:     print out_mem   ...:     def __init__(self):   ...:         inner_mem = 'inner_mem'   ...:         self.inner_mem = 'self.inner_mem'   ...:         self._inner_mem = 'self._inner_mem'   ...:         self.out_mem = 'self.out_mem'    ...:     In [2]: out = ClassOut()In [3]: out.out_memIn [4]: out._inner_memIn [5]: out.inner_memIn [6]: class ClassOut:   ...:     out_mem = 'out_mem'   ...:     print out_mem   ...:     def __init__(self):   ...:         inner_mem = 'inner_mem'   ...:         self.inner_mem = 'self.inner_mem'   ...:         self._inner_mem = 'self._inner_mem'   ...:         self.out_mem1 = 'self.out_mem1'   ...:         out_memIn [7]: out = ClassOut()In [8]: out.out_memIn [9]: ClassOut.out_memIn [10]: ClassOut.out_mem1In [11]: out._inner_memIn [12]: out.out_mem = 'out_mem modified by object'In [13]: ClassOut.out_memIn [14]: out.out_memIn [15]: out.new_mem = 'clas'In [16]: out.new_memIn [17]: o = ClassOut()In [18]: o.out_memIn [19]: ClassOut.out_memIn [20]: ClassOut.out_mem = 'out_mem modified by Class'In [21]: o.out_mem

 

 

 考察:

1. python解释器处理解释class

2. 类的初始化定义方法

3. 类的__init__方法 与 class object成员定义, class instance成员的定义

4. 类定义成员时的命名约定

5. class object  与 class instance的区别

6. class object 与 class instance在处理缺失成员访问时查询域优先级的区别

7. class 定义 __private_mem 不被外部发现的元婴? (被重命名为_[class_name]__private_mem)!!!详见如下代码!

 

In [48]: class ClassOut:

    ...: out_mem = 'out_mem'

    ...: print out_mem

    ...: def __init__(self):

    ...: inner_mem = 'inner_mem'

    ...: self.inner_mem = 'self.inner_mem'

    ...: self._inner_mem = 'self._inner_mem'

    ...: self.out_mem1 = 'self.out_mem1'

    ...: self.__private_mem = 'self.__private_mem'

    ...:

out_mem

 

In [49]: no = ClassOut()

 

In [50]: no.__private_mem

---------------------------------------------------------------------------

AttributeError Traceback (most recent call last)

<ipython-input-50-36dd351a1b65> in <module>()

----> 1no.__private_mem

 

AttributeError: ClassOut instance has no attribute '__private_mem'

 

In [51]: no._ClassOut__private_mem

Out[51]: 'self.__private_mem'

 

 

 

 

 

>>> obj = object()>>> obj.name = "whatever"Traceback (most recent call last):  File "
", line 1, in
AttributeError: 'object' object has no attribute 'name'

但是为什么这样就可以呢:

>>> class Object(object):pass...>>> Obj = Object()>>> Obj.name = "whatever">>> Obj.name'whatever'>>>

答: 现在你给第二个代码块中的Object加上属性 __slots__ 试试:

>>> class Object(object):...     __slots__ = {}...>>> Obj = Object()>>> Obj.name = "whatever"Traceback (most recent call last):  File "
", line 1, in
AttributeError: 'Object' object has no attribute 'name'

会发现抛出了同样的异常。 object 、 list 、 dict 等内置函数都如此。

拥有 __slots__ 属性的类在实例化对象时不会自动分配 __dict__ ,而obj.attr 即 obj.__dict__['attr'], 所以会引起 AttributeError

对于拥有 __slots__ 属性的类的实例 Obj 来说,只能对 Obj 设置__slots__ 中有的属性:

>>> class Object(object):...     __slots__ = {
"a","b"}...>>> Obj = Object()>>> Obj.a = 1>>> Obj.a1>>> Obj.c = 1Traceback (most recent call last): File "
", line 1, in
AttributeError: 'Object' object has no attribute 'c'

详细见 

转载于:https://www.cnblogs.com/ToDoToTry/p/3658429.html

你可能感兴趣的文章
从数据集输出艺术家
查看>>
Linux云计算面试会被问到的常见问题,linux运维课程学习
查看>>
Spring Boot实践教程:开篇
查看>>
感动世界的英雄妈妈
查看>>
DML语言DDL
查看>>
专题讨论:敏捷软件开发和传统软件工程
查看>>
深入探究VC —— 链接器link.exe(4)【转】http://blog.csdn.net/wangningyu/article/details/4849452...
查看>>
mfc的WM_PAINT笔记
查看>>
苹果应用商店AppStore审核规则指南
查看>>
第五节课-神经网络2
查看>>
使用idea启动node项目的问题
查看>>
mysql 存储过程
查看>>
0627.selenium请求库*1
查看>>
How To Ask Questions The Smart Way 转
查看>>
Bash变量(二):位置变量与预定义变量
查看>>
C++中的空类,默认产生哪些类成员函数?
查看>>
SSH中懒加载异常的解决办法
查看>>
终极 Shell
查看>>
CALayer 2 详解 -----转自李明杰
查看>>
git gc
查看>>