獲取對象類型:
金鳳ssl適用于網站、小程序/APP、API接口等需要進行數據傳輸應用場景,ssl證書未來市場廣闊!成為創新互聯的ssl證書銷售渠道,可以享受市場價格4-6折優惠!如果有意向歡迎電話聯系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!
一、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類型
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
當前標題:pythonClass:獲取對象類型
地址分享:http://vcdvsql.cn/article48/pdhshp.html
成都網站建設公司_創新互聯,為您提供企業建站、品牌網站設計、App設計、品牌網站制作、網站設計、靜態網站
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯