【python深入】获取对象类型及属性
2018-02-22 18:39
841 查看
在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']
什么是属性?属性包括变量和方法~~~
参考:
https://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/0013868200480395edcd8f8987a4871b01b5e340bbb8223000
查看类型可以通过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']
什么是属性?属性包括变量和方法~~~
参考:
https://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/0013868200480395edcd8f8987a4871b01b5e340bbb8223000
相关文章推荐
- java获取对象属性类型、属性名称、属性值
- Python面向对象编程(类和实例 访问限制 继承和多态 获取对象信息 实例属性和类属性)
- Python中继承、多态、多继承、判断类型、json.load()解析、获取/设置对象信息
- Python帮助函数调试函数 用于获取对象的属性及属性值
- Python脚本控制的WebDriver 常用操作 <十八> 获取测试对象的css属性
- 深入源码解析Python中的对象与类型
- Python深入03 对象的属性
- java获取对象属性类型、属性名称、属性值
- java获取对象属性类型、属性名称、属性值
- Python深入03 对象的属性
- python动态获取对象的属性和方法
- 【Python】[面性对象编程] 获取对象信息,实例属性和类属性
- 深入源码解析Python中的对象与类型
- python动态获取对象的属性和方法
- 使用表达式树访问对象、类型及成员(下):获取对象和属性的值
- python元编程_获取类或对象的属性
- C#泛型、反射-根据对象名获取该类型以及其属性
- python面对对象编程-------5:获取属性的四种办法:@property, __setattr__(__getattr__) ,descriptor
- Python 继承、多重继承、多态、isinstance判断类型、type()获取变量类型、dir()获取变量的所有属性
- java获取对象属性类型、属性名称、属性值