python Class:获取对象类型

发布时间:2020-06-17 08:54:45

来源:51CTO

阅读:4220

作者:虎皮喵的喵

获取对象类型:

一、type

#!/usr/bin/env python3

# -*- coding: utf-8 -*-

class Animal(object):

def __init__(self, name, score):

self.name = name

self.score = score

def run(self):

print 'Animal is run'

class Dog(Animal):

def run(self):

print 'Dog is run'

print type(dog.run)

print type(Animal)

import types #导入模块types

print type('abc')==types.StringType #判断'abc'是否为字符串类型

print type(u'abc')==types.UnicodeType

print type([])==types.ListType

print type(int)==type(str)==types.TypeType   #所有的类型都是TypeType

二、isinstance类型

对于继承关系class,用isinstance最为方便。

#!/usr/bin/env python3

# -*- coding: utf-8 -*-

class Animal(object):

def __init__(self, name, score):

self.name = name

self.score = score

def run(self):

print 'Animal is run'

class Dog(Animal):

def run(self):

print 'Dog is run'

print isinstance(dog, Dog) and isinstance(dog, Animal)

三、attr类型getattr()getattr(object, name[, default])¶

Return the value of the named attribute of object.  name must be a string.

If the string is the name of one of the object’s attributes, the result is the

value of that attribute.  For example, getattr(x, 'foobar') is equivalent tox.foobar.  If the named attribute does not exist, default is returned if

provided, otherwise AttributeError is raised.

对象的状态存在,则返回状态值,若不存在,则返回AttributeError:信息

#!/usr/bin/env python3

# -*- coding: utf-8 -*-

class Animal(object):

def __init__(self, name, score):

self.name = name

self.score = score

def run(self):

print 'Animal is run'

class Dog(Animal):

def run(self):

print 'Dog is run'

dog = Dog('Pity', 98)

dog.run()

print getattr(dog, 'name')

print getattr(dog, 'run')

print getattr(dog, 'd')

2.hasattr()hasattr(object, name)¶

The arguments are an object and a string.  The result is True if the string

is the name of one of the object’s attributes, False if not. (This is

implemented by calling getattr(object, name) and seeing whether it raises an

exception or not.)

参数是对象和字符串,如果字符串是对象中的,返回True,否则返回False

#!/usr/bin/env python3

# -*- coding: utf-8 -*-

class Animal(object):

def __init__(self, name, score):

self.name = name

self.score = score

def run(self):

print 'Animal is run'

class Dog(Animal):

def run(self):

print 'Dog is run'

dog = Dog('Pity', 98)

print hasattr(dog, 'color')

3.setattr()setattr(object, name, value)¶

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 tox.foobar = 123.

设置属性变量

#!/usr/bin/env python3

# -*- coding: utf-8 -*-

class Animal(object):

def __init__(self, name, score):

self.name = name

self.score = score

def run(self):

print 'Animal is run'

class Dog(Animal):

def run(self):

print 'Dog is run'

dog = Dog('Pity', 98)

setattr(dog, 'color', '0xff00ff')

print dog.color

python 获取对象类型_python Class:获取对象类型相关推荐

  1. python语言的内置对象类型_Python内置对象类型

    核心数字类型: 数字:int,long,float,complex,bool 字符:str,unicode 列表:list 字典:dict 元组:tuple 文件:file 其他类型:集合(set), ...

  2. python字典查找元素_python字典获取元素

    广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! 环境依赖python 2.7安装sdk安装 sdk 的方式有两种:pip 安装和 ...

  3. python 获取方法名_python 动态获取当前运行的类名和函数名的方法

    一.使用内置方法和修饰器方法获取类名.函数名 python中获取函数名的情况分为内部.外部,从外部的情况好获取,使用指向函数的对象,然后用__name__属性 复制代码代码如下: def a():pa ...

  4. python requests下载网页_Python requests 获取网页一般的方法

    主要记录使用 requests 模块获取网页源码的方法 class Crawler(object): """ 采集类 """ def __i ...

  5. python调用pyc文件_Python之code对象与pyc文件(二)

    创建pyc文件的具体过程 前面我们提到,Python在通过import或from xxx import xxx时会对module进行动态加载,如果没有找到相应的pyc或dll文件,就会在py文件的基础 ...

  6. python属性和局部变量_python类与对象1

    1.类的基本用法 2.封装继承多态 3.特殊属性和方法 1.类的基本用法 1.1 类的定义与常用术语 1.2 类的创建使用和构造方法 1.3 self 1.4 类属性和实例属性 1.5 局部变量 1. ...

  7. python标准函数有哪些_Python几个标准类型内建函数

    Python提供了一些内建函数用于基本对象类型:cmp(),repr(),str(),type()和等同于repr()的(' ')操作符 (1)type() type的用法如下: type(objec ...

  8. 用python画小鸭_python中的鸭子类型(协议)和接口

    定义 1. 接口(interface) 学过静态语言的同学一定对接口的概念并不陌生,它类似于我们家里用的插座标准,要用电器时,我们得先查看对应的插头是否和插座孔匹配.也正因为此,接口给我们提供了很大的 ...

  9. python语法错误类型_python常见报错类型和异常处理

    更新ing 常见的报错类型和简析.异常处理.其他类型的报错(异常)类型简介.自定义异常 常见的报错类型和简析: 报错类型 报错内容 错误类型判断 错误解决方式 AttributeError 属性错误: ...

  10. python中的引用类型_Python中的值类型与引用类型

    其实各个标准资料中没有说明Python有值类型和引用类型的分类,这个分类一般是C++和Java中的.但是语言是相通的,所以Python肯定也有类似的.实际上Python 的变量是没有类型的,这与以往看 ...

最新文章

  1. gcc for Windows 开发环境介绍
  2. 如何用ipad制作精美的视频,并发布到视频网站
  3. C# Task的简单使用
  4. matlab中now函数_now()方法以及JavaScript中的示例
  5. android浏览器插件开发,【转】Chrome扩展开发自己的浏览器插件
  6. 源码安装php时出现configure: error: xml2-config not found. Please check your libxml2 installation...
  7. 怎么判断第几范式例题_学完就忘、做题就懵!初级会计怎么备考才能更高效?...
  8. AcWing 285. 没有上司的舞会(树形DP)
  9. 空心等腰三角形java_java打印输出任意大小的等腰三角形,实心菱形,空心菱形,平行四边形...
  10. 5G的调制方式,到底是怎么实现的?
  11. 2020大学计算机答案,超星2020大学计算机基础答案 全
  12. 2022年6月青少年软件编程(Python)等级考试试卷(一级)
  13. hexo(sakura)仿gitee添加文章贡献度日历图(echarts)
  14. java把URL转换成二维码并保存在指定的位置
  15. 计算机网络之五层体系结构
  16. JDBC连接Mysql并统计指定关键词在某一列中出现的次数
  17. 暑期实习Day7---SpringMVC
  18. atof()函数实现
  19. RAD Studio破解补丁合集(2018.12.25)
  20. python显示前几行数据_python读取文件的前几行

热门文章

  1. qq机器人升级最新教程
  2. CrossApp应用源码集合贴
  3. SanDisk U盘加密软件 在其他u盘使用
  4. app自动化之混合应用的测试(h5,小程序)
  5. Mac乐谱制作工具---Sibelius 8 for Mac西贝柳斯
  6. 华为OJ之奥运会金牌排名显示国家名称
  7. 最新伯乐PHP个人在线自动发卡网源码V3.1
  8. android mp4视频下载测试地址亲测有效
  9. 蓝桥杯c语言试题 历届真题 天干地支【第十一届】【决赛】【A组】
  10. iOS在应用中添加自定义字体