首页 > 解决方案 > 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更改NoneAttributeError: 'NoneType' object has no attribute 'substitute'.

标签: pythonstringooptypes

解决方案


推荐阅读