美文网首页
30.1-魔术方法—dir的作用

30.1-魔术方法—dir的作用

作者: BeautifulSoulpy | 来源:发表于2019-12-22 10:28 被阅读0次

生命必须要有裂缝,阳光才能照射进来!

面向对象部分高级部分主要集中在它的一些特殊的方法和属性,这是Python的精华部分,这些东西是其他语言所没有的;

我们把它分类,先一点一点的学;慢慢演化;

特殊属性

属性 方法
_module_ 类定义所在的模块名,返回string
_name_ 类、函数、方法等的名字
_class_ 对象或类所属的类
_bases_ 类的基类的元组,顺序为它们在基类列表中出现的顺序
_doc_ 类、函数的文档字符串,如果没有定义则为None
_mro_ 类的mro,class.mro()返回的结果的保存在 mro
_dict_ 类或实例的属性,可写的字典
t.py  _ t
class A:pass
def b():
    def c():
        pass
    return c

print(A.__module__)
print(b.__module__)
c = b()
print(c.__module__)
# --------------------------------------------------------
__main__
__main__
__main__

查看属性dir

通过dir了解模块里面有什么,函数里面有什么,类里面有什么;

_dir_ :调用dir()时触发。
作用:返回一个序列,包含对象的所有成员并进行排序;隐藏对象的某些属性;

class A:pass
class B(A):pass
class C(B,A):pass

print(C.__dict__)   # 返回字典对象;
print(dir(C))  # 尽可能收集属性;

#-------------------------------------------------------------
{'__module__': '__main__', '__doc__': None}
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']

C._dict_ 与C.dir().dir(C)

dir(c) 与c.__dict__ 等价;
class A:pass
class B(A):pass
class C(B,A):
    def __init__(self):
        self.x = 100
#     def __dir__(self):
#         return ('a','b','c','d')
    def printdir(self):
        print(dir(),'~~~~~~')
c = C()
print(dir(c))
print(sorted(c.__dir__()))
#--------------------------------------------------------------------
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'printdir', 'x']
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'printdir', 'x']

dir(obj)对于不同类型的对象obj具有不同的行为:
如果对象是模块对象,返回的列表包含模块的属性名和变量名。
如果对象是类型或者类对象,返回的列表包含类的属性名,及它的基类的属性名。
如果obj不写,返回列表包含内容不同:
在模块中,返回模块的属性和变量名
在函数中,返回本地作用域的变量名
在方法中,返回本地作用域的变量名

未开启带参dir

dir(obj)参数obj不包含方法_dir_(),该方法将最大化限度的收集属性信息;

class A:pass
class B(A):pass
class C(B,A):
    def __init__(self):
        self.x = 100
c = C()   # 实例化
print(dir(c))   # 有x
#------------------------------------------------------
{'__module__': '__main__', '__init__': <function C.__init__ at 0x000002DB24F827B8>, '__doc__': None}
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']

开启带参数dir

dir(obj)参数obj包含方法_dir_(),该方法被调用,
1.必须返回可迭代对象; 字典返回Key;

class A:pass
class B(A):pass
class C(B,A):
    def __init__(self):
        self.x = 100
    def __dir__(self):
        return ('a','b','c','d')
c = C()
print(dir(c))
#-----------------------------------------
['a', 'b', 'c', 'd']
开启无参数dir
class A:pass
class B(A):pass
class C(B,A):
    def __init__(self):
        self.x = 100
    def __dir__(self):
        return ('a','b','c','d')
    def printdir(self):
        print(dir(),'~~~~~~')
c = C()
print(dir(c))
print('-'*60)
print(dir())
print('-'*60)

def b(a,b,c):
    print(dir(),'~')
b(1,2,3)
print(c.printdir())  
#-------------------------------------------------------------------------------------
['a', 'b', 'c', 'd']
------------------------------------------------------------
['A', 'B', 'C', 'In', 'NamespaceMagics', 'Out', '_', '_Jupyter', '__', '___', '__builtin__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', '_dh', '_getshapeof', '_getsizeof', '_i', '_i1', '_i10', '_i11', '_i12', '_i13', '_i14', '_i15', '_i16', '_i17', '_i18', '_i19', '_i2', '_i20', '_i21', '_i3', '_i4', '_i5', '_i6', '_i7', '_i8', '_i9', '_ih', '_ii', '_iii', '_nms', '_oh', 'autopep8', 'b', 'c', 'exit', 'get_ipython', 'getsizeof', 'json', 'np', 'quit', 'var_dic_list']
------------------------------------------------------------
['a', 'b', 'c'] ~
['self'] ~~~~~~
None
官方手册

相关文章

网友评论

      本文标题:30.1-魔术方法—dir的作用

      本文链接:https://www.haomeiwen.com/subject/oflinctx.html