在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

【python深入】获取对象类型及属性的更多相关文章

  1. python selenium 获取对象输入的属性值

    .get_attribute("value") from selenium import webdriver import time driver=webdriver.Firefo ...

  2. python基础——获取对象信息

    python基础——获取对象信息 当我们拿到一个对象的引用时,如何知道这个对象是什么类型.有哪些方法呢? 使用type() 首先,我们来判断对象类型,使用type()函数: 基本类型都可以用type( ...

  3. 获取对象类型(swift)

    获取对象类型(swift) by 伍雪颖 let date = NSDate() let name = date.dynamicType println(name) let string = &quo ...

  4. NX二次开发-UFUN获取对象的显示属性(图层,颜色,空白状态,线宽,字体,高亮状态)UF_OBJ_ask_display_properties

    NX9+VS2012 #include <uf.h> #include <uf_modl.h> #include <uf_obj.h> UF_initialize( ...

  5. python动态获取对象的属性和方法 (转载)

    首先通过一个例子来看一下本文中可能用到的对象和相关概念. #coding:utf-8 import sys def foo():pass class Cat(object): def __init__ ...

  6. python动态获取对象的属性和方法

    http://blog.csdn.net/kenkywu/article/details/6822220首先通过一个例子来看一下本文中可能用到的对象和相关概念.01     #coding: UTF- ...

  7. python动态获取对象的属性和方法 (转)

    转自未知,纯个人笔记使用 首先通过一个例子来看一下本文中可能用到的对象和相关概念. #coding:utf-8 import sys def foo():pass class Cat(object): ...

  8. Java反射获取对象VO的属性值(通过Getter方法)

    有时候,需要动态获取对象的属性值. 比如,给你一个List,要你遍历这个List的对象的属性,而这个List里的对象并不固定.比如,这次User,下次可能是Company. e.g. 这次我需要做一个 ...

  9. Python面向对象-获取对象信息type()、isinstance()、dir()

    type() type()函数用于判断对象类型: >>> type(11) <class 'int'> >>> type('abc') <clas ...

随机推荐

  1. for break

    public static void main(String[] args) { aaa: for (int j = 0; j < 2; j++) { System.out.println(&q ...

  2. Docker之 默认桥接网络与自定义桥接网卡

    docker引擎会默认创建一个docker0网桥,它在内核层连通了其他的物理或虚拟网卡,这就将所有容器和宿主机都放到同一个二层网络. 1. docker如何使用网桥 1.1 Linux虚拟网桥的特点 ...

  3. Firebird 烂笔头(一)

    下载非安装版,将文件解压缩到D:\FireBird2.5下面.然后里面有.bat文件,选择自己适合的类型安装后,在服务里面会有一个firebirdserver开头的服务,右键启动. win+R,在命令 ...

  4. git项目提交后执行添加忽略操作

    需要删除文件暂存区中的忽略文件 git rm -r --cached 需要忽略的已提交文件或文件夹 eg: git rm -r --cached target/

  5. new 对象时的暗执行顺序

    为什么称为暗执行顺序,因为当我们在new 对象时,其不是简简单单的new一个完事,它要首先检查父类的,静态的,非静态的等代码,就好像我们结婚生孩子一样,要先到祖宗那里,公安局那里,左邻右舍那里,告诉他 ...

  6. 算法实践--最小生成树(Kruskal算法)

    什么是最小生成树(Minimum Spanning Tree) 每两个端点之间的边都有一个权重值,最小生成树是这些边的一个子集.这些边可以将所有端点连到一起,且总的权重最小 下图所示的例子,最小生成树 ...

  7. [UE4]Button

    一.按钮有4种状态:Normal(普通状态).Hovered(鼠标悬停状态).Pressed(鼠标按下状态).Disabled(禁用状态),可以分别给每种状态设置样式. 二.按钮有如图所示的5个事件, ...

  8. 涨知识:equals 和 == 你真的了解吗?

    基本概念 ==是运算符,比较的是两个变量是否相等: equals()是Object方法,用于比较两个对象是否相等 看一下源码: public boolean equals(Object anObjec ...

  9. WordPress版微信小程序开发系列(一):WordPress REST API

    自动我发布开源程序WordPress版微信小程序以来,很多WordPress站长在搭建微信小程序的过程中会碰到各种问题来咨询我,有些问题其实很简单,只要仔细看看我写的文章,就可以自己解决.不过这些文章 ...

  10. HTML翻转菜单练习

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...