【python3.X】【基础】类的默认属性和方法

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言:未完待续

Python每创建一个新的类,会伴随很多属性和方法以self.__<attribute>__格式出现。
本文主要讨论类默认的attributes和 method.
以下建立两个类:

class Person:
	"""人类"""
    alive = True
    def __init__(self):
        self.name = 'dog'
        self.height = 1.8

    def get_location(self):
        location = 'China'
        self.location = location

    def _get_location(self):
        _location = 'China'
        self._location = _location

    def __get_location(self):
        __location = 'China'
        self.__location = __location


class Worker(Person):
	"""工人"""
    def __init__(self):
        self.card_id = 1

    def get_task(self):
        task = '搬砖'
        self.task = task

    def _get_task(self):
        _task = '搬砖'
        self._task = _task

    def __get_task(self):
        __task = '搬砖'
        self.__task = __task

一、_attribute_

1._annotations_:不适用

不适用

2._class_:例化后的类名

实例化后返回对应的类名。

def show_attribute(attribute):
    print('%s=======' %str(attribute.__name__))
    print('\t%s' %attribute)
    print('\ttype: %s' %type(attribute))
    
if __name__ == '__main__':
    name = Person()
    worker = Worker()
    show_attribute(Person.__class__)
    show_attribute(Worker.__class__)
    show_attribute(name.__class__)
    show_attribute(worker.__class__)
==>运行结果
	type=======
		<class 'type'>
		type: <class 'type'>
	type=======
		<class 'type'>
		type: <class 'type'>
	Person=======
		<class '__main__.Person'>
		type: <class 'type'>
	Worker=======
		<class '__main__.Worker'>
		type: <class 'type'>

3._dict_:字典形式的部分属性及方法

以字典的形式输出类__init__()中的属性,类的magic属性,类的方法。
注意双下划线属性和方法返回的key值,由__get_task变为了_Worker__get_task

def show_attribute(attribute):
    print('%s=======' %('__dict__'))
    keys = sorted(attribute.keys())
    for k in keys:
        print('\t%s: %s' %(k, attribute.get(k)))
    print('\ttype: %s' %type(attribute))

if __name__ == '__main__':
    name = Person()
    worker = Worker()
    show_attribute(Person.__dict__)
    show_attribute(Worker.__dict__)
    show_attribute(name.__dict__)
    show_attribute(worker.__dict__)
==>
	__dict__=======
		_Person__get_location: <function Person.__get_location at 0x101E88E0>
		__dict__: <attribute '__dict__' of 'Person' objects>
		__doc__: None
		__init__: <function Person.__init__ at 0x101E89B8>
		__module__: __main__
		__weakref__: <attribute '__weakref__' of 'Person' objects>
		_get_location: <function Person._get_location at 0x101E8928>
		alive: True
		get_location: <function Person.get_location at 0x101E8970>
		type: <class 'mappingproxy'>
	__dict__=======
		_Worker__get_task: <function Worker.__get_task at 0x101E87C0>
		__doc__: None
		__init__: <function Worker.__init__ at 0x101E8898>
		__module__: __main__
		_get_task: <function Worker._get_task at 0x101E8808>
		get_task: <function Worker.get_task at 0x101E8850>
		type: <class 'mappingproxy'>
	__dict__=======
		height: 1.8
		name: dog
		type: <class 'dict'>
	__dict__=======
		card_id: 1
		type: <class 'dict'>

奇淫巧计:动态生成新属性

class Team:
    def __init__(self):
        self.team_name = '划水队'

    def add_member(self, name):
        self.__dict__[name] = name

if __name__ == '__main__':
	team = Team()
    team.add_member('SpiderMan')
    print(team.SpiderMan)
    show_attribute(team.__dict__)
==>
	SpiderMan
	__dict__=======
		SpiderMan: SpiderMan
		team_name: 划水队
		type: <class 'dict'>

4._doc_:注解

类的注解

def show_attribute(attribute):
    print('%s=======' %str(attribute))
    print('\t%s' %attribute)
    print('\t%s' %type(attribute))


if __name__ == '__main__':
    name = Person()
    worker = Worker()
    show_attribute(Person.__doc__)
    show_attribute(Worker.__doc__)
    show_attribute(name.__doc__)
    show_attribute(worker.__doc__)
==>
	人类=======
		人类
		type: <class 'str'>
	工人=======
		工人
		type: <class 'str'>
	人类=======
		人类
		type: <class 'str'>
	工人=======
		工人
		type: <class 'str'>

5._module_:

6._slots_:

二、_method_()

1._delattr_()

2._dir_()

3._eq_()

4._format_()

5._getattribute_()

6._hash_()

7._init_subclass_()

8._ne_()

9._new_()

10._reduce_()

11._reduce_ex_()

12._repr_()

13._setattr_()

14._sizeof_()

15._str_()

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值