python - OOP 形式的“模板”
问题描述
我想以 OOP 方式使用 string.Template 但出现错误
from string import Template
class Foo:
def __init__(self):
self.template()
self.bar = type
def template(self):
self.bar = Template('$who')
def get_result(self):
print(self.bar.substitute(who="Me"))
some = Foo()
some.get_result()
但我收到一条错误消息
AttributeError: type object 'type' has no attribute 'substitute'
。如果将其数据类型self.bar
更改None
为
AttributeError: 'NoneType' object has no attribute 'substitute'
.
解决方案
推荐阅读
- python - Python shell 总是启动 2 个“python”进程
- stripe-payments - 处理连接用户入职状态的每个排列的逻辑?
- google-cloud-platform - 最初如何在没有容器 URL 的情况下创建一个空的 Cloud Run 服务?
- c++ - QSet 附加自定义对象
- netsuite - 如何从 python 调试 Netsuite restlet
- uml - 类似的用例。怎么做更好的解决方案?
- python - 是否有用于自动填充数字和对象列的 NULL 值的 python 函数?
- python - 如何从字典中的列表中获取所有值,我需要获取每个项目的标题
- java - 如何正确使用JMH?ArrayList 示例
- websocket - 未将流连接到房间 - 蚂蚁媒体服务器