zoukankan      html  css  js  c++  java
  • dir函数

    dir函数:

      dir() 是一个内置函数,用于列出对象的所有属性及方法

      下面进行尝试:

      用下面两个tests test2文件做实验

      

      

    #创建一个类,两个常量,类中函数test1,类中属性,
    class DirTest(object):
        def __init__(self):
            self.name = 'self.zym'
        name = 'zym'
    
        def test1(self):
            print('test1')
    
    
    
    NAME = "zym"
    TEST = 'zmds'
    
    
    res = DirTest()
    tests
    from web import tests
    
    print(dir(tests.res))
    print('_____')
    
    print(dir(tests))
    tests2

    然后执行test2文件,结果如下

    ['__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__', 'name', 'test1']
    
    _____
    
    
    ['DirTest', 'NAME', 'TEST', 'TestCase', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'res']
    
    
    结果如上:因为dir()传入的对象不同,横线上方是传入一个实例,他会打印出实例的所有,属性,test1实例化类中的函数,name为属性。
    
    横线下方是传入tests这个文件,他会打印出里面的变量,DirTest是类,NAME,TEST是变量,res是DirTest实例化出来的类。

    用途:目前在整合setting文件中用到过,将两个setting文件,搞到一块。

    例如:

            for items2 in dir(m2):
                if items2.isupper():
                    k2 = items2
                    v2 = getattr(m2,k2)
                    setattr(self,items2,v2)
    将m2中的配置搞到当前文件中。
  • 相关阅读:
    PV、UV、VV、IP是什么意思?
    多用户远程linux
    实用性阅读指南读书笔记
    在VS添加万能头文件 操作
    sqrt函数实现之卡马克方法
    大端法、小端法及其判断方法
    STL源码剖析之ROUND_UP函数实现原理及其拓展
    海康和多益面经
    小米实习生面试面经
    阿里c++一面面经
  • 原文地址:https://www.cnblogs.com/taozizainali/p/9697226.html
Copyright © 2011-2022 走看看