当前位置: 首页 > news >正文

下载资料免费网站信息流广告公司排名

下载资料免费网站,信息流广告公司排名,广西建设职业技术学院教育网站,怎样查公司注册信息查询TypeError: unbound method 错误通常发生在类方法被调用时,但没有正确绑定到实例。这通常意味着你试图在类本身上调用一个实例方法,或者没有使用正确的方式创建类实例。 1、问题背景 某位开发者在尝试创建一个类似于经典的 Pratt 递归下降解析器时遇到了…

TypeError: unbound method 错误通常发生在类方法被调用时,但没有正确绑定到实例。这通常意味着你试图在类本身上调用一个实例方法,或者没有使用正确的方式创建类实例。

在这里插入图片描述

1、问题背景

某位开发者在尝试创建一个类似于经典的 Pratt 递归下降解析器时遇到了 “TypeError: unbound method” 的错误。在简化了代码之后,开发者发现问题出在对中缀运算符的处理上,具体来说是 infix_led 函数没有正确绑定到 symbol 类的实例。

2、解决方案:

有两种解决方案:

方法1:
tokenize() 函数中,每当遇到一个非数字的运算符时,不直接生成一个 symbol 类,而是生成一个 symbol() 的实例。这确保了每个运算符都有一个单独的实例,可以绑定各自的 led 函数。

方法2:
使用 types.MethodType 函数将 infix_led 函数绑定到 symbol 类的实例上。这是一种更显式的绑定方式,也确保了每个运算符都有一个单独的实例,可以绑定各自的 led 函数。

代码示例:

class Symbol_base(object):""" A base class for all symbols"""id = None # node/token type namevalue = None #used by literalsfirst = second = third = None #used by tree nodesdef nud(self):""" A default implementation for nud """raise SyntaxError("Syntax error (%r)." % self.id)def led(self,left):""" A default implementation for led """raise SyntaxError("Unknown operator (%r)." % self.id)def __repr__(self):if self.id == "(name)" or self.id == "(literal)":return "(%s %s)" % (self.id[1:-1], self.value)out = [self.id, self.first, self.second, self.third]out = map(str, filter(None,out))return "(" + " ".join(out) + ")"symbol_table = {}
def symbol(id, bindingpower=0):""" If a given symbol is found in the symbol_table return it.If the symblo cannot be found theni create the appropriate classand add that to the symbol_table."""try:s = symbol_table[id]except KeyError:class s(Symbol_base):passs.__name__ = "symbol:" + id #for debugging purposess.id = ids.lbp = bindingpowersymbol_table[id] = selse:s.lbp = max(bindingpower,s.lbp)return sdef infix(id, bp):""" Helper function for defining the symbols for infix operations """def infix_led(self, left):self.first = leftself.second = expression(bp)return selfsymbol(id, bp).led = infix_led#define all the symbols
infix("+", 10)
symbol("(literal)").nud = lambda self: self #literal values must return the symbol itself
symbol("(end)")token_pat = re.compile("\s*(?:(\d+)|(.))")def tokenize(program):for number, operator in token_pat.findall(program):if number:symbol = symbol_table["(literal)"]s = symbol()s.value = numberyield selse:symbol = symbol_table.get(operator)if not symbol:raise SyntaxError("Unknown operator")yield symbol()symbol = symbol_table["(end)"]yield symbol()def expression(rbp = 0):global tokent = tokentoken = next()left = t.nud()while rbp < token.lbp:t = tokentoken = next()left = t.led(left)return leftdef parse(program):global token, nextnext = tokenize(program).nexttoken = next()return expression()def __main__():print parse("1 + 2")if __name__ == "__main__":__main__()

在这个代码示例中,tokenize() 函数生成 Symbol_base 类的实例,并且使用 types.MethodType() 函数将 infix_led 函数绑定到每个实例上。这样就确保了每个运算符都有一个单独的实例,并且他们的 led 函数都正确绑定到了各自的实例上。

如果你遇到 TypeError: unbound method 错误,请确保你正确地实例化类,并通过实例调用方法。如果确实需要通过类调用方法,请使用 @classmethod@staticmethod 装饰器。

希望这个解释和解决方案对你有所帮助。

http://www.wooajung.com/news/33172.html

相关文章:

  • 如何用花生壳做网站百度网页广告怎么做
  • 科技公司内蒙古网站制作推广普通话手抄报内容50字
  • 搜索引擎网站建设代码seo公司厦门
  • 网站如何测试有无未做链接的文件seo网站营销推广公司
  • 外贸流程图片seo日常优化内容是什么
  • 网站后台数据分析怎么做2022网站快速收录技术
  • 网站做商标在那类宁波seo推广费用
  • wordpress网站分析如何快速搭建网站
  • 上海网站建设公司凡科建站客服电话
  • wordpress运行php夫唯seo教程
  • 网站建设制作公司地址灰色关键词怎么做排名
  • jsp动态网站开发实践教程电子档电脑培训机构
  • 网站可以做赌博广告网站维护公司
  • 做pc端网站价格seo推广技术
  • 重庆seo网站建设微信推广链接怎么制作
  • 深业资本有限公司网站建设成都爱站网seo站长查询工具
  • 百度站长工具有哪些百度搜索引擎seo
  • 代理网络游戏服务平台克州seo整站排名
  • 做网站都要掌握什么长沙seo网络营销推广
  • 做ps的素材哪个网站2021年10月新闻摘抄
  • 视觉中国的图片可以拿来做网站软件优化
  • qq空间的网站广州推广系统
  • 建设银行人力资源招聘网站怎么注册网站免费的
  • 深圳学校网站建设报价制作网站建设入门
  • 大连 网站制作营销网络是什么
  • 求html码源网站电商seo搜索引擎优化
  • 高端网站建设 南京网站seo具体怎么做?
  • 中邮保险网站购物网站页面设计
  • 做一个营销型网站有哪些内容seo关键词怎么优化
  • 清远哪里有网页设计培训学费seo整站优化什么价格