Published: 10 Aug 2018 › Updated: 10 Aug 2018
PyH源码解析(3)
添加Tag对象内容
通过构造函数传递Tag内容
用户使用场景如下:
>>> a=div('test')
>>> print(a)
<div>
test
</div>
实现原理
Tag对象继承自list对象,list对象中存放的是Tag对象的内容或者子Tag对象,所以,新增这些内容或者子Tag对象,只是简单的调用addObj函数,而这个函数也只是简单的调用list的append函数,增加到列表中。如下:
def addObj(self, obj):#{{{
| 44 id = self.setID(obj)
| 45 setattr(self, id, obj)
| 46 self.append(obj)
| 47 #}}}
至于setID()和setattr()的作用,我们后续再讲。
打印Tag对象的子Tag和内容
知道了Tag对象和子Tag和内容存放在list中,就知道如何打印他们了,核心代码如下所示:
for c in self:
88 if isinstance(c, Tag):
89 result += c.render()
90 else:
91 result += c
Leave PyH源码解析(3) to:
Read more #cn posts
Best Posts From brysj22952
We have not curated any of brysj22952's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From brysj22952
- 我为什么想自己实现语法解析器
- PyH源码解析(6)——完结篇
- PyH源码解析(5)
- PyH源码解析(4)
- PyH源码解析(3)
- PyH源码解析(2)
- PyH源码解析(1)
- Python语言PyH模块生成HTML文档使用说明
- linux屏蔽ctrl+s键的方法
- LXML官方文档翻译
- 使用Flask和React构建博客系统(1)
- 解决网站开发中网页在手机上显示不全的问题
- 2018.6.25 My microblog 微博
- MyBrain项目(1)
- 2018.3.20 My microblog 微博
- click的命令自动补齐功能介绍
- python命令行神器click介绍
- Linux find命令使用心得(Linux find cmd use experience)
- Piston工具翻译-Steem网络的瑞士军刀(2)
- Piston工具翻译-Steem网络的瑞士军刀(1)