更有效地调用 Python 中的方法

发布于 2024-11-27 01:15:13 字数 2366 浏览 1 评论 0原文

我是一个正在学习 Python the Hard Way 的新手。

本练习的重点是编写一个单词扫描仪,以便在通过提供的单元测试运行时通过鼻子测试。

在以下提供的单元测试上运行nosetests时,我收到此错误:

`TypeError:必须使用词典实例作为第一个参数调用未绑定方法scan()(改为获取str实例)

课程提供的测试

from nose.tools import *
from ex48 import lexicon

def test_directions():
    assert_equal(lex.scan("north"), [('direction', 'north')])
    result = lex.scan("north south east")
    assert_equal(result, [('direction', 'north'),
                          ('direction', 'south'),
                          ('direction', 'east)])

之后一些调查我发现这里有一个用户正在进行相同的练习:

nosetests,python

python 变量,类

那里的答案建议在单元测试中实例化(实例化?)该方法。所以我做了以下修改,并在文件 ex48.py 中编写了我的类,并且它通过了鼻子测试。

修改测试

from nose.tools import *
from ex48 import lexicon


def test_directions():
    lex = lexicon("north")
    assert_equal(lex.scan("north"), [('direction', 'north')])
    lex = lexicon("north south east")
    result = lex.scan("north south east")
    assert_equal(result, [('direction', 'north'),
                          ('direction', 'south'),
                          ('direction', 'east')]) 

ex48.py - Scanner

class lexicon(object):

    def __init__(self, data):
        #nosetests fails me if I don't put in some dummy
        # __init__ function with a dummy line, not sure why.
    self.direction = data

    def scan(self, data):
        split_data = data.split()
        directions = ['north', 'south', 'east', 'west']
        data_reply = []
        #for loop for the length of the list
        for split_data_item in split_data:
            #If data is in the directions list
            if split_data_item in directions:
                #Add [('direction', data)] to a dict
                data_reply.append(('direction', split_data_item))

        #Return the list
        return data_reply

我不确定单元测试是否要更改。我在这里找到了有关“直接实例化对象”的线索:

Python :“直接”调用方法是否会实例化对象?

但我不确定这是否适用。扫描仪是否可以实例化自身,或者提供的单元测试是否是一个技巧“问题”并且必须进行修改?

I'm a newb working through Learn Python the Hard Way.

The point of this exercise is to write a word scanner for that passes the nosetests when run by a provided unit test.

While running nosetests on the following provided unit test I was getting this error:

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

Lesson-supplied Test

from nose.tools import *
from ex48 import lexicon

def test_directions():
    assert_equal(lex.scan("north"), [('direction', 'north')])
    result = lex.scan("north south east")
    assert_equal(result, [('direction', 'north'),
                          ('direction', 'south'),
                          ('direction', 'east)])

After some investigation I found here a user who is working through the same exercise:

nosetests, python

python variables, classes

The answer there suggested instantiating (instaniating?) the method inside the unit test. So I did the following modification and wrote up my class in file ex48.py and it passes nosetests.

Modified Test

from nose.tools import *
from ex48 import lexicon


def test_directions():
    lex = lexicon("north")
    assert_equal(lex.scan("north"), [('direction', 'north')])
    lex = lexicon("north south east")
    result = lex.scan("north south east")
    assert_equal(result, [('direction', 'north'),
                          ('direction', 'south'),
                          ('direction', 'east')]) 

ex48.py - Scanner

class lexicon(object):

    def __init__(self, data):
        #nosetests fails me if I don't put in some dummy
        # __init__ function with a dummy line, not sure why.
    self.direction = data

    def scan(self, data):
        split_data = data.split()
        directions = ['north', 'south', 'east', 'west']
        data_reply = []
        #for loop for the length of the list
        for split_data_item in split_data:
            #If data is in the directions list
            if split_data_item in directions:
                #Add [('direction', data)] to a dict
                data_reply.append(('direction', split_data_item))

        #Return the list
        return data_reply

I'm not sure if the unit test was meant to be changed. I found a clue about 'directly intantiating an object' here:

Python: does calling a method 'directly' instantiate the object?

But am not sure if this applies. Can a scanner be made to instantiate itself or is the provided unit test a trick 'question' and must be modified?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自由如风 2024-12-04 01:15:13

Learn Python The Hard Way的在线版本中,他们有:

def test_directions():
    assert_equal(lexicon.scan("north"), [('direction', 'north')])
    result = lexicon.scan("north south east")
    assert_equal(result, [('direction', 'north'),
                          ('direction', 'south'),
                          ('direction', 'east')])

对于测试,这表明你不需要词典类,而是需要一个带有扫描功能的 lexicon.py 文件来测试。

它被拼写为实例化(就像我正在创建此类的实例一样)。

In the online version of Learn Python The Hard Way they have:

def test_directions():
    assert_equal(lexicon.scan("north"), [('direction', 'north')])
    result = lexicon.scan("north south east")
    assert_equal(result, [('direction', 'north'),
                          ('direction', 'south'),
                          ('direction', 'east')])

for the test, which suggests that you don't need a lexicon class, but a lexicon.py file with a scan function to test.

And it's spelled instantiating (as in I'm making an instance of this class).

花开柳相依 2024-12-04 01:15:13

您应该保持测试原样,并在扫描方法上使用 @staticmethod 装饰器。这样,您将能够直接从类调用该方法,而无需为此实例化对象。

class lexicon(object):

   @staticmethod
   def scan(data):
      #do the stuff here

You should keep the test the way it was, and use a @staticmethod decorator on the scan method. This way, you'll be able to call the method directly from the class without need to instanciate an object for this.

class lexicon(object):

   @staticmethod
   def scan(data):
      #do the stuff here
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文