#In simple form
def name(name):
print(name)
name='arjun'
name(name) #In this line, what's happening?
#Error 'str' object is not callable
Asked
Active
Viewed 33 times
0

C.Nivs
- 12,353
- 2
- 19
- 44

Karma Tseriing
- 15
- 3
-
1You've renamed `name` from a function to a `str`. Call that string something else and it will fix your problem – C.Nivs Jun 02 '20 at 15:24
-
1You replaced the function `name` with a string, and you can't call a string as if it is a function. – kindall Jun 02 '20 at 15:24
-
`name` is no more a function name but a string instead with the value `'arjun'` – Saadi Toumi Fouad Jun 02 '20 at 15:25
-
Does this answer your question? [Python Variable Declaration](https://stackoverflow.com/questions/11007627/python-variable-declaration) – khachik Jun 02 '20 at 15:26
1 Answers
2
There are not separate namespaces for functions and other objects. If you write
def name1(name2):
print(name2)
you create an instance of type function
, then assign that instance to the name name1
. (A def
statement is just a very fancy kind of assignment statement.)
Parameter names are local variables, belonging to the scope defined by the function, not the scope in which the function is defined. That means you can reuse the name, though it isn't recommended as it can be confusing.
belongs to the scope in which the function is defined
|
| +--- belongs to the scope inside the function
| |
v v
def name(name):
print(name)
However, the following assignment
name = 'arjun'
is in the same scope as the function definition, so this makes name
refer to a str
object instead of the function
object it used to refer to.

chepner
- 497,756
- 71
- 530
- 681