python变量,类 [英] python variables, classes

查看:177
本文介绍了python变量,类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Dear All,试图学习python,类,以及如何在变量之间传递变量。在这里学习指南,并遇到麻烦与以下错误:

Dear All, Trying to learn python, classes, and how to pass variables between. Going through a learning guide here, and am having trouble with the following Error:

TypeError: unbound method scan() must be called with lexicon instance as first argument (got str instance instead)

有人可以帮助我更好地理解这一点吗?
THANKS !!!

Can someone please help me understand this better? THANKS!!!

class lexicon (object):
  def __init__(self,data):
    self.direction = data
    self.words = data.split()

  def scan(self):
    return self.words

def main():
    stuff = raw_input('> ') 
    x = lexicon.scan(stuff)

if __name__ == '__main__':
 main()


推荐答案

在您可以调用其中一个方法之前键入 lexicon 。即

You have to instantiate an object of type lexicon before you can invoke one of its methods. i.e.

lex = lexicon(data)
lex.scan()

这篇关于python变量,类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆