程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

Python的内置类属性

发布于2020-02-26 11:57     阅读(1423)     评论(0)     点赞(0)     收藏(0)


一、__slots__

  1. 类中没有__slots__属性时,可以进行动态添加属性
class Person(object):
    def __init__(self, name, age):
        self.name = name
        self.age = age


p = Person('lucy', 18)
p.city = '上海'
print(p.city)  # 上海

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  1. 当类中有__slots__时,可以对属性进行控制。只可以设置__slots__中的属性,如果设置的属性不在范围内,会报错
class Person(object):
    __slots__ = ('name', 'age', 'sex')

    def __init__(self, name, age):
        self.name = name
        self.age = age


p = Person('lucy', 18)
p.sex = '男'
p.city = '上海'  # 报错
print(p.sex)  # 男

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

二、__doc__

  1. 类的文档信息
class Person(object):
    """
    这是类的文档信息
    """
    __slots__ = ('name', 'age', 'sex')

    def __init__(self, name, age):
        self.name = name
        self.age = age


p = Person('lucy', 18)
print(p.__doc__)  # 对象名.__doc__
print(Person.__doc__)  # 类名.__doc__
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

三、__module__

  1. 获取对象所在的模块

test.py中的代码如下:

class Person(object):
    __slots__ = ('name', 'age', 'sex')

    def __init__(self, name, age):
        self.name = name
        self.age = age


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

demo.py中的代码:

import test
p = test.Person('lucy', 18)
print(p.__module__)  # test
  • 1
  • 2
  • 3

四、__class__

  1. 获取对象的类名(全称__main__.类名
class Person(object):
    __slots__ = ('name', 'age', 'sex')

    def __init__(self, name, age):
        self.name = name
        self.age = age


p = Person('lucy', 18)
print(p.__class__)  # <class '__main__.Person'>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

五、__dict__

  1. 以字典的形式,显示对象所有的属性
class Person(object):

    def __init__(self, name, age):
        self.name = name
        self.age = age


p = Person('lucy', 18)
print(p.__dict__)  # {'name': 'lucy', 'age': 18}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

六、__dir__()

  1. dir(对象名)等价。查看一个对象支持的所有属性和方法
class Person(object):
    __slots__ = ('name', 'age')

    def __init__(self, name, age):
        self.name = name
        self.age = age


p = Person('lucy', 18)
print(p.__dir__())
print(dir(p))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

七、__setitem__方法

  1. 不能直接把一个对象当做字典来使用
class Person(object):
    def __init__(self, name, age, city):
        self.name = name
        self.age = age
        self.city = city
        
p = Person('张三', 18, '襄阳')
# 我想把age修改成20,会报错
p['age'] = 20  # [] 语法会调用对象的 __setitem__方法
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  1. 利用__setitem__方法,可以进行修改和添加属性
class Person(object):
    def __init__(self, name, age, city):
        self.name = name
        self.age = age
        self.city = city

    def __setitem__(self, key, value):
        self.__dict__[key] = value

        
p = Person('张三', 18, '襄阳')
p['age'] = 20  
p['sex'] = '男'
print(p.__dict__)  # {'name': '张三', 'age': 20, 'city': '襄阳', 'sex': '男'}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

八、__getitem__方法

class Person(object):
    def __init__(self, name, age, city):
        self.name = name
        self.age = age
        self.city = city

    def __getitem__(self, item):
        return self.__dict__[item]
    
p = Person('张三', 18, '襄阳')
print(p['name'])  # 张三    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

九、__delitem__方法

class Person(object):
    def __init__(self, name, age, city):
        self.name = name
        self.age = age
        self.city = city

    def __delitem__(self, key):
        print(f'删除的属性{key}')
        
p = Person('张三', 18, '襄阳')
del p['city']  # 删除的属性city        
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
发布了21 篇原创文章 · 获赞 37 · 访问量 3万+


所属网站分类: 技术文章 > 博客

作者:黎明要到来了

链接:https://www.pythonheidong.com/blog/article/234098/e5c8e3c0328d710dc2a6/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

0 0
收藏该文
已收藏

评论内容:(最多支持255个字符)