獲取對象類型:
創(chuàng)新互聯(lián)建站從2013年創(chuàng)立,先為渭濱等服務(wù)建站,渭濱等地企業(yè),進行企業(yè)商務(wù)咨詢服務(wù)。為渭濱企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。一、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 #導(dǎo)入模塊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類型
對于繼承關(guān)系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.
對象的狀態(tài)存在,則返回狀態(tài)值,若不存在,則返回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.)
參數(shù)是對象和字符串,如果字符串是對象中的,返回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
.
設(shè)置屬性變量
#!/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
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
網(wǎng)頁名稱:pythonClass:獲取對象類型-創(chuàng)新互聯(lián)
網(wǎng)站鏈接:http://vcdvsql.cn/article26/cdiccg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、域名注冊、面包屑導(dǎo)航、網(wǎng)站建設(shè)、商城網(wǎng)站、全網(wǎng)營銷推廣
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容