Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6216183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:11:21+00:00 2026-05-24T07:11:21+00:00

I’m a newb working through Learn Python the Hard Way. The point of this

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T07:11:21+00:00Added an answer on May 24, 2026 at 7:11 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.