Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中的装饰器设计模式_Python - Fatal编程技术网

Python中的装饰器设计模式

Python中的装饰器设计模式,python,Python,我正在创建一个Flask网站,我想根据您当前的页面显示不同的注销链接,即 如果我们在主页上并登录,请将此链接包装在h2标记中 如果我们在另一个页面并登录,请将此链接包装在下划线标记中 如果我们已登录,请将此链接包装在强标记中 到目前为止我已经试过了 class HtmlLinks(): html ="" def set_html(self, html): self.html = html def get_html(self): ret

我正在创建一个Flask网站,我想根据您当前的页面显示不同的注销链接,即

  • 如果我们在主页上并登录,请将此链接包装在h2标记中
  • 如果我们在另一个页面并登录,请将此链接包装在下划线标记中
  • 如果我们已登录,请将此链接包装在强标记中
到目前为止我已经试过了

class HtmlLinks():
    html =""
    def set_html(self, html):
        self.html = html

    def get_html(self):
        return self.html

    def render(self):
        print(self.html)


class LogoutLink(HtmlLinks):
    def __init__(self):
        self.html = "Logout"

class LogoutLinkH2Decorator(HtmlLinks):
    def __init__(self, logout_link):
        self.logout_link = logout_link
        self.set_html("<h2> {0} </h2>").format(self.logout_link.get_html())

    def call(self, name, args):
        self.logout_link.name(args[0])

class LogoutLinkUnderlineDecorator(HtmlLinks):
    def __init__(self, logout_link):
        self.logout_link = logout_link
        self.set_html("<u> {0} </u>").format(self.logout_link.get_html())

    def call(self, name, args):
        self.logout_link.name(args[0])

class LogoutLinkStrongDecorator(HtmlLinks):
    def __init__(self, logout_link):
        self.logout_link = logout_link
        self.set_html("<strong> {0} </strong>").format(self.logout_link.get_html())

    def call(self, name, args):
        self.logout_link.name(args[0])

logout_link = LogoutLink()
is_logged_in = 0
in_home_page = 0

if is_logged_in:
    logout_link = LogoutLinkStrongDecorator(logout_link)
if in_home_page:
    logout_link = LogoutLinkH2Decorator(logout_link)
else:
    logout_link = LogoutLinkUnderlineDecorator(logout_link)

logout_link.render()

我做错了什么以及如何纠正它。请提供帮助。

set\u html
不返回任何内容,但您尝试对其返回值调用
format
方法


self.set\u html(“{0}”).format(self.logout\u link.get\u html())
set\u html
不返回任何内容,但您尝试对其返回值调用
format
方法


self.set\u html(“{0}”).format(self.logout\u link.get\u html())

因此您有几行代码如下:

    self.set_html("<h2> {0} </h2>").format(self.logout_link.get_html())
self.set\u html(“{0}”).format(self.logout\u link.get\u html())
您可能希望它们看起来像:

    self.set_html("<h2> {0} </h2>".format(self.logout_link.get_html()))
self.set\u html(“{0}.format(self.logout\u link.get\u html()))

因此您有几行代码如下所示:

    self.set_html("<h2> {0} </h2>").format(self.logout_link.get_html())
self.set\u html(“{0}”).format(self.logout\u link.get\u html())
您可能希望它们看起来像:

    self.set_html("<h2> {0} </h2>".format(self.logout_link.get_html()))
self.set\u html(“{0}.format(self.logout\u link.get\u html()))

看起来您希望
set\u html
有一个返回值(不同于
None
)?或者结尾)太靠左边了。另外,不确定你的背景,但是你有html作为类属性。您希望它是一个对象属性。要做到这一点,只需注释掉写有
html=“
”的行。谢谢@CorleyBrigmanIt,看起来您希望
set\u html
有一个返回值(不同于
None
)?或者结尾)太靠左边了。另外,不确定你的背景,但是你有html作为类属性。您希望它是一个对象属性。要做到这一点,只需注释掉
html=”“
。谢谢@CorleyBrigman