提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
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'>