26

Why can't I call the function again? Or, how can I make it?

Suppose I have this function:

def a(x, y, z):
 if x:
     return y
 else:
     return z

and I call it with:

print a(3>2, 4, 5)

I get 4.

But imagine that I declare a variable with the same name that the function (by mistake):

a=2

Now, if I try to do:

a=a(3>4, 4, 5)

or:

a(3>4, 4, 5)

I will get this error: "TypeError: 'int' object is not callable"

Is it not possible to assign the variable 'a' to the function?

6
  • well, the error explains itself. You have redefined a to be an int instead of a function; Now, a is a variable (instead of a function); hence the consequence Commented Mar 4, 2014 at 2:35
  • I'm not sure what are you asking. Are you asking why can't you call a number? Commented Mar 4, 2014 at 2:35
  • No, what I just want to know is: if it's possible to after assign "a=2", there is some way to "a" point again to the function. Commented Mar 4, 2014 at 2:49
  • While this seems obvious to people who write Python all day, coming from a C# background this is annoying. I may have a 'private' variable named self.a, but want to have a 'public' function called a(units='g') that converts the units on the private variable 'a' to whatever the caller wants. Instead, I find I need to write my 'public' function as def format_a(units='g'): Commented Jun 17, 2016 at 13:44
  • 1
    Good question, in the end, from the answers, contrary to other languages, you can't have a getter like def x(self): return self.x, you need to name the getter differently, e.g. get_x(self). I came here because after a refactoring of my getter name, a working code was just not working anymore. Commented Dec 25, 2018 at 14:39

3 Answers 3

35

After you do this:

a = 2

a is no longer a function, it's just an integer (you reassigned it!). So naturally the interpreter will complain if you try to invoke it as if it were a function, because you're doing this:

2()
=> TypeError: 'int' object is not callable

Bottom line: you can't have two things simultaneously with the same name, be it a function, an integer, or any other object in Python. Just use a different name.

Sign up to request clarification or add additional context in comments.

2 Comments

Is this behavior due to the fact, that everything is an object in python (integers and functions are both handled as objects?) Or does this hold true to most other programming languages?
It's true for languages where 1) functions are first-class citizens and 2) types are dynamic; in a statically typed language you cannot reassign say, a number to a variable that was declared to store a function.
9

names in Python are typically identifiers for a specific type, more like naming a box which stores a variable/function/method or any object in Python. When you are reassigning, you are just renaming a box.

You can find that out by doing the below.

Initially, a is assigned a value 9, at location 140515915925784. As soon as I use the same identifier for a function , a now refers to a box containing the address of that function in 4512942512

Reassigning a to 3 again points a to refer to a different address.

>>> a = 9
>>> id(a)
140515915925784
>>> def a(x):
...     return x
...
>>> id(a)
4512942512
>>> a
<function a at 0x10cfe09b0>
>>>
>>>
>>>
>>> a = 3
>>> id(a)
140515915925928
>>> a
3
>>>

Comments

5

You're assigning the name a to a function definition, and then reassigning it to an integer.

It's syntactically correct, but it's not what you want.

It's best to give functions semantic names that describe what you're doing with the arguments being passed to them, and to give variables semantic names that describe what object they're pointing to. If you do that, you'll have more readable code and you certainly won't make this mistake again.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.