亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

python Class:獲取對象類型

發布時間:2020-06-17 08:54:45 來源:網絡 閱讀:4491 作者:虎皮喵的喵 欄目:編程語言

獲取對象類型:

一、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:獲取對象類型


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

句容市| 垦利县| 和政县| 明星| 武胜县| 革吉县| 同江市| 清镇市| 滨海县| 余庆县| 宜兰市| 灵丘县| 佛教| 毕节市| 南丹县| 武强县| 十堰市| 贡山| 申扎县| 门头沟区| 海伦市| 肇州县| 大石桥市| 泰安市| 崇仁县| 林州市| 高邮市| 忻州市| 盐边县| 法库县| 滦平县| 瑞丽市| 平顶山市| 临漳县| 黔江区| 金寨县| 特克斯县| 韶山市| 察哈| 瑞金市| 资中县|