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

  • Home
  • SEARCH
  • 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 7969279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:12:44+00:00 2026-06-04T07:12:44+00:00

Iam trying to do the exercises in the book Learn Python the hard way

  • 0

Iam trying to do the exercises in the book “Learn Python the hard way” page: 106. The example is below:

cities = {'CA': 'San Francisco', 'MI': 'Detroit', 'FL': 'Jacksonville'}

cities['NY'] = 'New York'
cities['OR'] = 'Portland'

def find_city(themap, state):
    if state in themap:
        return themap[state]
    else:
        return "Not found."

# ok pay attention!
cities['_find'] = find_city

while True:
    print "State? (ENTER to quit)",
    state = raw_input("> ")
    if not state: break

    # this line is the most important ever! study!
    city_found = cities['_find'](cities, state)
    print city_found

I do not understand what cities['_find'] = find_city does? What is _find? In particular, why the underscore? Similarly, I am not sure what city_found = cities['_find'](cities, state) does. I have seen a similar post on the same question:
learn python the hard way exercise 40 help

which basically says that cities['_find'] = find_city adds the function find_city to the dictionary, but I still dont understand what city_found = cities['_find'](cities, state) does(?)

I’d really appreciate if someone could explain me the above two lines. Thanks for your time.

  • 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-06-04T07:12:46+00:00Added an answer on June 4, 2026 at 7:12 am

    This code:

    cities['_find'] = find_city
    

    simply inserts the function find_city into the cities dictionary, using the key _find. The underscore has no particular meaning, it’s just part of the key string. Probably chosen to not collide with actual city names.

    This code:

    city_found = cities['_find'](cities, state)
    

    Calls the find_city function, by first looking it up in the dictionary using the _find key.

    It could be rewritten as:

    city_found = find_city(cities, state)
    

    There doesn’t seem to be any real point in doing it like this, there’s no benefit in having the dictionary (which is called a “map” in the code) contain the find function, that I can see.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to complete Exercise 47 of Learn Python The Hard Way, 2nd
iam trying to get all object's xpath's from loaded page via selenium something similar
iam trying play animation with xcode,in specefic Time for example after 3 minutes play
I am trying to solve some prolog exercises in order to learn the language
I am currently trying to learn a bit of Cocoa (using the book Cocoa
I am trying to learn C++ myself by reading a book and do exercise
Iam trying to return data ( the stored exercises information : Name , period
I'm trying to work my way through the exercises at the bottom of this
Iam trying to create an iterative server based on datagram sockets (UDP). It calls
Iam trying to retrieve data from mysql database into stylesheet.php but it is not

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.