python判断对象类型_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)

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

print type(Animal)

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

import types #导入模块types

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

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

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

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

print type([])==types.ListType

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

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

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

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

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

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

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

print getattr(dog, 'name')

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

print getattr(dog, 'run')

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

print getattr(dog, 'd')

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

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

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

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

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

原文地址:http://blog.51cto.com/13502993/2147109

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值