Zed Shaw Learn Python Hard Tutorial

I am new to programming and am currently doing exercises in the Zed Shaw Python book. In Zed Ex41, there is such a function:

def runner(map, start): next = start while True: room = map[next] print "\n-------" next = room() 

My question is, why did he have to designate the "beginning" of the variable "next" when he could immediately "start"? Why didn't he just do it?

 def runner(map, start): while True: room = map[start] print "\n-------" start = room() 

Because this function also works. Thanks

+4
source share
4 answers

The second example works, yes, but he is trying to write a stylebook in Python style, and I think the first one speaks more clearly about what is happening. start , because the name of the variable makes no sense when it is no longer the actual start , but instead of next , which we enter.

+18
source

I think this was done for readability. In a programmer’s skill, start should be the beginning of something. next was supposed to represent the next element.

You are correct that the code may be shortened, but it distorts the value of start .

Note that in current versions of Python (2.6 or later), next is a built-in function, so it is no longer recommended to specify the next variable.

+11
source

I think this is just for readability. When writing code, you should always remember that variables (as well as functions, classes, etc.) must always have meaningful names, otherwise reading your code will be a nightmare. The value of the variable in the loop is to hold the next element, not the start element.

+2
source

He did not have to, but did it for name style reasons.

0
source

Source: https://habr.com/ru/post/1334427/


All Articles