python Class:获取对象类型
admin
2023-07-23 23:02:44
0

获取对象类型:

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

python Class:获取对象类型

print type(Animal)

python Class:获取对象类型



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

python Class:获取对象类型

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

python Class:获取对象类型

print type([])==types.ListType

python Class:获取对象类型

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

python Class:获取对象类型



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

python Class:获取对象类型


三、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()

python Class:获取对象类型

print getattr(dog, 'name')

python Class:获取对象类型

print getattr(dog, 'run')

python Class:获取对象类型

print getattr(dog, 'd')

python Class:获取对象类型



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

python Class:获取对象类型


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 Class:获取对象类型


相关内容

热门资讯

吴尊因航班延误后转机,行李丢失... 6月25日,男演员吴尊因航班延误多次转机,行李丢失三天没有找到,发文控诉国泰航空:“我已经很有耐心了...
老人微信有77万条未读消息,塞... 6月23日,广东网友小林(化名)发现奶奶手机上的微信竟有77万条未读消息,顿时不知所措,随即在社交平...
美媒:世界正降低对霍尔木兹海峡... 据《纽约时报》报道,在伊朗与美国、以色列战争期间,伊朗一度关闭霍尔木兹海峡,但国际社会通过增加输油管...
“不可接受”!苹果商店下架多款... 【环球网报道 记者 闫珮云】据路透社报道,在俄罗斯互联网公司VK旗下多款应用程序被美国苹果公司的应用...
风云对话 | 太空医疗研究:我... 6月23-25日,第十七届夏季达沃斯在大连举办,论坛主题为 “规模化创新”。全球局势纷繁复杂,德国太...
多人将两女子和狗“浸猪笼”游街... 6月25日,有网友发布视频称,湖南岳阳汨罗市一街道上出现多名男女当街拉人“浸猪笼”游行。视频画面显示...
驻委内瑞拉使馆提醒中国公民防范... 驻委内瑞拉使馆提醒在委中国公民密切关注当地地震预警和灾害相关信息,防范余震及地震导致的次生灾害;保持...
空调自清洁可以中断吗 空调自清洁是可以中断的。空调自清洁功能是独立按键,在空调清洁完成前,再次按下自清洁即可结束,如出现不...
前锋燃气灶自动熄火是什么原因引... 造成燃气灶熄火的原因有几种:天然气不足:燃气灶燃烧一段时间出现自动熄灭,同时会出现噔噔能的响声,可能...
燃气灶放上锅以后就自动熄火的原... 主要原因是,如果出现燃气灶不放锅空烧的时候不会熄火,放上锅之后才熄火,那么原因是燃气灶燃烧时火苗无法...