专栏首页python3python Class:获取对象类型

python Class:获取对象类型

获取对象类型:

一、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类型

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

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

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • Notes for python (2)

    #!/usr/bin/python # Filename: using_tuple.py zoo = ('wolf', 'elephant', 'pe...

    py3study
  • python 面向对象之类的继承

    AttributeError: 'WoMan' object has no attribute 'go_to_work'

    py3study
  • 格式化输出

    比如要求用户输入用户名和年龄,然后打印如下格式:My name is xxx,my age is xxx.

    py3study
  • python 字符串所有操作

    使用type获取创建对象的类 type(name) 使用dir获取类的成员dir(name) 使用vars获取类的成员和各个成员的值

    用户7886150
  • Scala语言基础之结合demo和spark讲实现链式计算

    一,什么是链式计算 1,一般开发习惯把事情封装到一个方法中;链式编程思想是把要做的事情封装到block中,给外部提供一个返回这个block的方法 2,链式编程思...

    Spark学习技巧
  • Neo4j-1.5 WHERE子句

    悠扬前奏
  • python变量的定义

            python中字符带单引号或者双引号,python都认为是字符串。

    py3study
  • Sentence-BERT详解

    BERT和RoBERTa在文本语义相似度(Semantic Textual Similarity)等句子对的回归任务上,已经达到了SOTA的结果。但是,它们都需...

    mathor
  • Kotlin 's Function 《Kotlin极简教程》正式上架:

    Functions are declared using the fun keyword, followed by a function name and an...

    一个会写诗的程序员
  • 自动段指导任务(Automatic Segment Advisor)

    本文主要介绍自动段指导(Automatic Segment Advisor)任务的内容进行详细介绍。

    TeacherWhat

扫码关注云+社区

领取腾讯云代金券