我在尝试调用类中定义的函数时遇到了TypeError类的问题。错误是:TypeError: p() takes exactly 1 argument (2 given)
class HTMLGen:
def p(text):
return ("<p>%s</p>" % text)
def a(text):
return ("<a>%s</a>" % text)
def b(text):
return ("<b>%s</b>" % text)
def title(text):
return ("<title>%s</title>" % text)
def comment(text):
return ("<!--%s-->" % text)
def div(text):
return ("<div>%s</div>" % text)
def span(text):
return ("<span>%s</span>" % text)
def body(text):
return ("<body>%s</body>" % text)
然后,在导入HTMLGen类并尝试以这种方式使用HTMLGen.p(t)
函数之后
>>> import htmlgen
>>> website = htmlgen.HTMLGen()
>>> paragraph = website.p("Hello World!")
按回车键,我得到前面提到的错误。有人知道为什么HTMLGen.p()和其他函数会得到多个参数吗?防止这种情况发生的最简单方法是什么
每当您在类内创建函数时,它必须在类内的所有函数中具有自参数
您需要添加一个额外的参数,按惯例称为self。它指的是对象本身。许多编程语言使用关键字
this
相关问题 更多 >
编程相关推荐