在python中,查看當前的對象所能夠調用的所有方法?
查看類型可以通過type,也可以通過isinstance方法,查看屬性可以通過dir()
下面是對type的介紹:
————》基本類型的判斷可以通過type來實現:
>>> type(123)
<class 'int'>
>>> type('a')
<class 'str'>
>>> type([])
<class 'list'>
>>> type({})
<class 'dict'>
>>> a = (1,2,3)
>>> type(a)
<class 'tuple'>
>>> type(None)
<class 'NoneType'>
>>> type(type(a))
<class 'type'>
可以通過import types,在2.X中可以通過types.ListType來判斷是否等於List的Type,可以查看:
>>> import types
>>> types.ListType
<type 'list'>
>>> dir(types)
['BooleanType', 'BufferType', 'BuiltinFunctionType', 'BuiltinMethodType', 'ClassType', 'CodeType', '
ComplexType', 'DictProxyType', 'DictType', 'DictionaryType', 'EllipsisType', 'FileType', 'FloatType'
, 'FrameType', 'FunctionType', 'GeneratorType', 'GetSetDescriptorType', 'InstanceType', 'IntType', '
LambdaType', 'ListType', 'LongType', 'MemberDescriptorType', 'MethodType', 'ModuleType', 'NoneType',
'NotImplementedType', 'ObjectType', 'SliceType', 'StringType', 'StringTypes', 'TracebackType', 'Tup
leType', 'TypeType', 'UnboundMethodType', 'UnicodeType', 'XRangeType', '__all__', '__builtins__', '_
_doc__', '__file__', '__name__', '__package__']
可以通過下面語句來判斷某個對象的類型是否屬於某個基礎類型:
>>> type('a')==types.StringType
True
但是在3.X中可以看到ListType、StringType等的已經去掉了
>>> import types
>>> dir(types)
['AsyncGeneratorType', 'BuiltinFunctionType', 'BuiltinMethodType', 'CodeType', 'CoroutineType', 'DynamicClassAttribute', 'FrameType', 'FunctionType', 'GeneratorType', 'GetSetDescriptorType', 'LambdaType', 'MappingProxyType', 'MemberDescriptorType', 'MethodType', 'ModuleType', 'SimpleNamespace', 'TracebackType', '_GeneratorWrapper', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_ag', '_calculate_meta', '_collections_abc', '_functools', 'coroutine', 'new_class', 'prepare_class']
也可以通過isinstance():在對類的繼承關系的判定上是支持的,並且也能夠支持基本類型的判定(且在2.X和3.X中均適用,測試用的是Python27和36)
>>> isinstance([], list)
True
>>> isinstance({}, dict)
True
>>> isinstance((), tuple)
True
>>> isinstance(1, int)
True
>>> isinstance(1, long)
False
>>> isinstance(111111111, long)
False
>>> a = test() #其中test為之前定義的一個類
>>> isinstance(a, object)
True
>>> isinstance(a, test2)
False
>>> isinstance(u'123', str)
False
>>> isinstance('123', str)
True
>>> isinstance(u'123', unicode)
True
下面是dir的使用:
例如下方class的定義中,直接dir(類名)則不包含對象所具備的屬性value,初始化一個對象a之后,則dir(a)中就包含了value的屬性
>>> class test4():
def __init__(self):
self.value = 1
>>> dir(test4)
['__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__']
>>> a = test4()
>>> dir(a)
['__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__', 'value']
那么如何為類定義一個屬性呢?可以直接類似下方這樣定義,這樣類和對象的屬性就都有value這一項
>>> class test5():
value = 1
def __init__(self):
pass
def getvalue(self):
print (value)
>>> dir(test5)
['__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__', 'getvalue', 'value']
>>> b = test5()
>>> dir(b)
['__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__', 'getvalue', 'value']
定義的時候 不用self,但是在方法中調用的時候可以用self,也可以不用self,如下:
>>> class test6():
value = 1
def __init__(self):
pass
def getvalue(self):
print (self.value)
>>> dir(test6)
['__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__', 'getvalue', 'value']
>>> c = test6()
>>> dir(c)
['__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__', 'getvalue', 'value']
什么是屬性?屬性包括變量和方法~~~
參考: