參考: https://docs.python.org/3/library/functions.html?highlight=hasattr#getattrhtml
例子1:針對類TestA 作屬性操做python
class TestA:
def fun_1(self):
print "fun_1"
def fun_2(self):
print "fun_2"
def fun_2():
print "fun_3"
test_a = TestA()
print hasattr(test_a, "fun_1")
fun_a = getattr(test_a,"fun_2")
print fun_a
fun_a()
setattr(test_a,"fun_2",fun_2) #對原有函數進行了覆蓋。熱更新代碼經常使用
test_a.fun_2()
print dir(TestA)
結果:app
True <bound method TestA.fun_2 of <__main__.TestA instance at 0x0000000002E70E88>> fun_2 fun_3 ['__doc__', '__module__', 'fun_1', 'fun_2']
例子2:針對一個文件內的屬性進行條件篩選:ide
首先建立新測試文件test_file.py:函數
A="a.b" class B: def fun_1(self): pass C=[1,2] D="png.a" E="cc.ee"
而後對文件進行邏輯處理:測試
# coding=utf-8 import test_file # 刪選出test_file中全部的str、內有.、不含有png的 str_list = [] for item in dir(test_file): # 查詢文件下的屬性名 value = getattr(test_file, item) # 查詢屬性名的具體值 if type(value) != str: continue if value.find('.') < 0 or value.find('png') >= 0: continue str_list.append(value) for item in str_list: print item
結果:ui
a.b cc.ee
總結:this
setattr
(object, name, value)spa
This is the counterpart of getattr()
. The arguments are an object, a string and an arbitrary value. The string may name an existing attribute or a new attribute. The function assigns the value to the attribute, provided the object allows it. For example, setattr(x, 'foobar', 123)
is equivalent to x.foobar = 123
.rest
setattr會進行屬性覆蓋,代碼熱更可使用。
dir
([object])
Without arguments, return the list of names in the current local scope. With an argument, attempt to return a list of valid attributes for that object.
If the object has a method named __dir__()
, this method will be called and must return the list of attributes. This allows objects that implement a custom __getattr__()
or __getattribute__()
function to customize the way dir()
reports their attributes.
If the object does not provide __dir__()
, the function tries its best to gather information from the object’s __dict__
attribute, if defined, and from its type object. The resulting list is not necessarily complete, and may be inaccurate when the object has a custom __getattr__()
.
The default dir()
mechanism behaves differently with different types of objects, as it attempts to produce the most relevant, rather than complete, information:
The resulting list is sorted alphabetically. For example:
>>> import struct >>> dir() # show the names in the module namespace ['__builtins__', '__name__', 'struct'] >>> dir(struct) # show the names in the struct module ['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__initializing__', '__loader__', '__name__', '__package__', '_clearcache', 'calcsize', 'error', 'pack', 'pack_into', 'unpack', 'unpack_from'] >>> class Shape: ... def __dir__(self): ... return ['area', 'perimeter', 'location'] >>> s = Shape() >>> dir(s) ['area', 'location', 'perimeter']
Note
Because dir()
is supplied primarily(首要的) as a convenience for use at an interactive prompt(敏捷交互?), it tries to supply an interesting set of names more than it tries to supply a rigorously or consistently defined set of names, and its detailed behavior may change across releases. For example, metaclass attributes are not in the result list when the argument is a class.
dir返回有意義的屬性集合,而不是全部屬性