0

I'm learning python 2.7, im trying to finish the basic in programming to moving on to python 3.0. I came across this code from my book Learn python the hard way- Zed A. Shaw:

from sys import exit
from random import randint

class Game(object):
    def __init__(self, start):
        self.quips =[
        "You died. You kidda suck at this."
        "Your mom would be proud.If she were smarter."
        "Such a luser."
        "I have a small puppy that's better at this."

         ]
        self.start = start
    def play(self):
        next = self.start

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

    def central_corridor(self)
    [ some other functions ]

    def death(self):
    print self.quips[randint(0, len(self.quips)-1)]
    exit(0)

a_game = Game("central_corridor")
a_game.play()

What is "next", I see it colored blue in my editor, is "next" something special? I lose track of the program's flow from the begining ( "next = self.start" confused me the most) , please help me.

2 Answers2

1

next is a name of an inbuilt function which is used in iterators. If you want to see it in use DataCamp has a good explanation: https://www.programiz.com/python-programming/methods/built-in/next

it is used as a variable in your example, but the IDE highlights it as such. Same as using list as a variable when list() is a function

usernamenotfound
  • 1,540
  • 2
  • 11
  • 18
  • That link refers to `next()` rather than `next`. Perhaps this is a Python 2 vs. Python 3 difference. In any event, it doesn't seem to answer OP's question, at least directly. – John Coleman Jan 24 '18 at 16:07
  • In the context of that code, `next` seems to be a local variable which is discarded before it is used. Perhaps it is a bug where the author of the code meant to use `yield`? – John Coleman Jan 24 '18 at 16:12
  • this is incomplete code, I didn't want to figure out what things mean – usernamenotfound Jan 24 '18 at 16:21
0

next is a name of build-in python function. Your IDE is most likely coloring it for that reason. In the context of example code it looks like it's just a variable with the same name.

Justinas Marozas
  • 2,482
  • 1
  • 17
  • 37