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 6391425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:40:12+00:00 2026-05-25T03:40:12+00:00

I was skimming through Learn python the hard way and I have come across

  • 0

I was skimming through Learn python the hard way and I have come across a code snippet like below. But I did not understand what’s behind it, a function basically returns a string that which it’s class has a function of the same name. And it can call the function properly.
Well, you will get what I mean when you look at the code.

Code:

from sys import exit
class Game(object):
    def __init__(self, start):
        self.start = start

    def play(self):
        next = self.start

        while True:
            print "\n--------"
            room = getattr(self, next)
            next = room()

    def a1(self):
        print "You are in a1, redirecting to a2"
        return 'a2'
    def a2(self):
        print "You are in a2, exiting"
        return 'death'
    def death(self):
        print 'You died'
        exit(0)


a_game = Game('a1')
a_game.play()
  • 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-25T03:40:13+00:00Added an answer on May 25, 2026 at 3:40 am

    What you see is the result of the getattr() builtin. Python is a high level language and what’s more, an interpreted one. Since all objects in python are basically constructed in memory in forms of hash-tables Python has a rather vast Reflection mechanism.

    This means that Python can inspect and modify its own classes and objects in runtime and one of the ways to do so is the getattr() builtin.
    The getattr builtin retrieves the value of the member in the first argument, which name it receives as a string in the second argument. getattr(x, "y") is basically the same as x.y, but getattr also allows for a safer approach where a default value can be given if a member is not actually present: getattr(x, "y", "No such member"). If x has no member y then x.y would cause an AttributeError exception. On the other hand gettattr(x, "y", "No such member") would return the string “No such member” and no exception is raised.

    In your case, the methods are placed in the next variable as strings and are accessed through the getattr() builtin placed into room. room() is then invoked and the next methods name is returned and stored into next. This repeats in the while loop until exit(0) is invoked in Game.death().

    You should also have a look at setattr() and hasattr()

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

Sidebar

Related Questions

I've currently skimming through the Python-bindings for Redland and haven't found a clean way
Lastly I was skimming through Underscore.js code to learn JavaScript idioms and I found
I was skimming through the 4sq API documentation but couldn't find any info about
I've recently been skimming through Irrlicht's 3D game engine source code, and I really
I've just started skimming 'Debugging MS .Net 2.0 Applications' by John Robbins, and have
I haven't officially started learning PHP, just skimming through a couple tutorials and I
I usually prefer to code with a black background and white/coloured text but I
We have a requirement that has had me skimming the web for quite sometime
I have SQL data that looks like this: events id name capacity 1 Cooking
Skimming through the SubModule tutorial , I created a submodule out of the boto

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.