纯基本功练习

1.get和set方法

2.property的使用

3.property的使用2

4.导入模块的默认导入路径

5.模块重新导入

6  ==  和 is

7.浅拷贝和深拷贝

  1 class We(object):
  2     def __init__(self):
  3         self.__num = 30
  4     def getNum(self):
  5         return self.__num
  6     def setNum(self,val):
  7         self.__num = val
  8 
  9 w = We()
 10 print(w.getNum())
 11 w.setNum(111)
 12 print(w.getNum())

 2.property的使用

  1 class We(object):
  2     def __init__(self):
  3         self.__num = 30
  4     def getNum(self):
  5         return self.__num
  6     def setNum(self,val):
  7         self.__num = val
  8     num = property(getNum,setNum)
  9 
 10 w = We()
 11 print(w.num)
 12 w.num = 100
 13 print(w.num)

3.property的使用2

  1 class We(object):
  2     def __init__(self):
  3         self.__num = 30
  4 
  5     @property
  6     def num(self):
  7         print("getter")
  8         return self.__num
  9 
 10     @num.setter
 11     def num(self,num):
 12         print("setter")
 13         self.__num = num
 14 
 15 w = We()
 16 print(w.num)
 17 w.num = 100
 18 print(w.num)

4

In [2]: import sys

In [3]: sys.path
Out[3]: 
['',
 '/usr/bin',
 '/usr/lib/python35.zip',
 '/usr/lib/python3.5',
 '/usr/lib/python3.5/plat-x86_64-linux-gnu',
 '/usr/lib/python3.5/lib-dynload',
 '/usr/local/lib/python3.5/dist-packages',
 '/usr/lib/python3/dist-packages',
 '/usr/lib/python3/dist-packages/IPython/extensions',
 '/home/python/.ipython']

In [4]: from imp import *

In [5]: reload(tt)
Out[5]: <module 'tt' from '/home/python/tt.py'>

6.

判断内容用==      判断是否同一个地址用is

-5 ~ 126   is  为True

7.浅拷贝和深拷贝

copy.copy()  它内部可以判断是否是可变类型

                    拷贝一个不可变类型(如:元组),就是同一个地址

                    拷贝如果是一个可变类型,只拷贝第一层

理由:元组就是不可变类型,拷贝出来多份有什么用?所以直接就是地址相同

浅拷贝就是指向同一个地址

深拷贝(内部原理是递归)

In [16]: import copy

In [17]: b = copy.deepcopy(a)

8.私有化

In [1]: from siyou import *

In [2]: num
Out[2]: 1

In [3]: _num
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-3-a3b2dccd37c6> in <module>()
----> 1 _num
NameError: name '_num' is not defined

结论,带下划线的变量导不进来

In [5]: import siyou

In [6]: siyou._num
Out[6]: 2

直接import 模块,这种情况下是可以用的

In [7]: class T(object):
   ...:     __num = 20
   ...:     

In [8]: t = T()

In [9]: t._
t._T__num           t.__dir__           t.__ge__            t.__init__          t.__ne__            t.__repr__          t.__subclasshook__
t.__class__         t.__doc__           t.__getattribute__  t.__le__            t.__new__           t.__setattr__       t.__weakref__
t.__delattr__       t.__eq__            t.__gt__            t.__lt__            t.__reduce__        t.__sizeof__        
t.__dict__          t.__format__        t.__hash__          t.__module__        t.__reduce_ex__     t.__str__        

如上所示,并没有__num,所以现在用dir(t)

In [9]: dir(t)
Out[9]: 
['_T__num',
...]

结论:实际上__num被改成了 _类名__num   (名字重整),其实是可以打印的(只知原理即可,不让用)

In [24]: t._T__num
Out[24]: 10

猜你喜欢

转载自blog.csdn.net/aoxue018/article/details/82432782