前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python Class:获取对象类型

python Class:获取对象类型

作者头像
py3study
发布2020-01-08 16:36:20
1.6K0
发布2020-01-08 16:36:20
举报
文章被收录于专栏:python3python3python3

获取对象类型:

一、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)

oob_type5.PNG
oob_type5.PNG

print type(Animal)

oob_type6.PNG
oob_type6.PNG

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

oob_type7.PNG
oob_type7.PNG

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

oob_type7.PNG
oob_type7.PNG

print type([])==types.ListType

oob_type7.PNG
oob_type7.PNG

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

oob_type7.PNG
oob_type7.PNG

二、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)

oob_type7.PNG
oob_type7.PNG

三、attr类型

  1. 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()

oob_type1.PNG
oob_type1.PNG

print getattr(dog, 'name')

oob_type2.PNG
oob_type2.PNG

print getattr(dog, 'run')

oob_type3.PNG
oob_type3.PNG

print getattr(dog, 'd')

oob_type4.PNG
oob_type4.PNG

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')

oob_has.PNG
oob_has.PNG

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

oob_set.PNG
oob_set.PNG
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档