I am comfused about the name behavior of Python in recursion

Ben Finney ben+python at benfinney.id.au
Wed Oct 5 22:48:09 EDT 2016


380162267qq at gmail.com writes:

> Google told me Python name is a label attaching to the object.

Well, “Google told me” has no necessary bearing on whether it's true :-)
so that's not a good citation to give.

If you need to know what Python terminology means, the Python
documentation is better.

    <URL:https://docs.python.org/3/glossary.html>

so, I don't think “name” has a technical meaning for Python.

Rather, Python has an assignment operation, which binds a name (or some
other reference) to an object.

> But in this recursive function,the name 'a' will point to different number object.
>
> def rec(a):
> 	a+=1
> 	if a<10:
> 		rec(a)
> 	print(a)
>
> rec(0) gives me 10....1 normally.

Yes. What would you expect instead, and why?

Maybe you think the *name* is passed around. That doesn't happen; in
Python, names are not tractable in that way.

By referencing the name ‘a’, you immediately have the object referred to
by that name. You don't have the name itself, and when you pass that
object, the name you happened to use does not come with it.

-- 
 \         “Science is a way of trying not to fool yourself. The first |
  `\     principle is that you must not fool yourself, and you are the |
_o__)               easiest person to fool.” —Richard P. Feynman, 1964 |
Ben Finney




More information about the Python-list mailing list