Python same variable name in different functions

I've been searching a while for my question but haven't found anything of use. My question is rather easy and straightforward.

Is it preferable or perhaps "pythonic" to use the same name for a certain variable which will appear in different functions, but with the same purpose?

For example:

def first_function():
    pt = win.getMouse() # This waits for a mouseclick in a graphical window.

    if blabla.button.clicked(pt):
        second_function()

def second_function():
    pt = win.getMouse() 

    if whatever.button.clicked(pt):
        third_function()

Does it matter if the variable reference (pt) to win.getMouse() in the second_function() has the same name as the variable in the first_function()? Or should the variable pt in the second function be named something else?

asked Dec 10, 2013 at 11:31

1

Names in functions are local; reuse them as you see fit!

In other words, the names in one function have no relationship to names in another function. Use good, readable variable names and don't worry about names clashing.

answered Dec 10, 2013 at 11:31

Martijn PietersMartijn Pieters

986k274 gold badges3877 silver badges3236 bronze badges

3

Its not about "Pythonic" or not. In programming you always wish your variables to have a meaning, if the same name occures in differend functions that means they do things with the same purpose or same params. Its fine to use same names in different functions, as long as they don't collide and make problems

answered Dec 10, 2013 at 11:34

Used to store or assign a value to a variable name

What are Python Variables?

Like other programming languages, Python variables allow us to store or assign a value to a variable name. It helps in applying programming logic to a variety of inputs.

Python same variable name in different functions

The article below will review how to define variables in Python, the correct syntax, and run simple experiments. By doing so, we can study their behavior and gain some insight into Python’s internal workings.

Defining Variables in Python

Python is a dynamically typed language, meaning Python recognizes the type of variable automatically. Therefore, the programmer does not need to define the data type when defining the variable. Python discerns the type of the variable from the value it is assigned. It is opposed to statically typed languages like C++ or JavaScript, which require the programmer to declare the type of the variable while defining it.

One advantage of a dynamically typed language like Python is that a single variable can be used for multiple data types. However, such a practice is not recommended.

A variable can be easily created in Python using the assignment (“=”) operator as shown below:

Python same variable name in different functions

Variable Names

Python syntax allows for a variety of variable names. Variable names are permitted to contain letters, numbers, and underscores. A variable cannot start with a number but can start with a letter or an underscore.

Python also uses a list of reserved keywords that hold a special meaning. For example, the word “def” is used to define functions. It is possible for variables to use keywords as names, but it is not good practice. Finally, a variable name should not include any spaces or tabs.

It is important to note that variable names are case-sensitive, which means that a variable named “x” is different from a variable named “X.”

Legal naming conventions are listed below:

  • _variable_name_
  • variablename
  • variable_name
  • variableName
  • VariableName
  • variablename1
  • variable_name_1
  • variablename_1

Scope

The scope of a variable can be thought of as the context within which the variable exists. It is an important concept because the value of a Python variable will depend on the scope of that variable.

When a variable is defined inside a function, it is possible for a variable with the same name to refer to two different values depending on the active scope of the program.

For example, the variable x contains the value 5 inside of the function. However, outside of the function, it takes the value 4. It’s important to know the scope in which the variable is defined. It is advisable to use unique variable names regardless of the scope. It can help to avoid confusion and unexpected errors.

The concept of scope is illustrated in the code snippet below:

Python same variable name in different functions

How Python Variables Work

Python variables are not what they seem. They do not hold values; instead, they hold references to objects. In Python, everything is an object, which means even the most basic data types like integers and strings are objects. It is different from C++ and other programming languages, where integers are standalone data types and not complex objects with associated properties and methods.

The fact that integer data types are objects can be verified using the type() function. Running the following command yields the type of variable x, which has been assigned the integer 4.

Python same variable name in different functions

When the variable x is defined and assigned a value of 4, an integer object is created in the memory with the value 4. Then, the variable x is given the address of where the object is in the memory. Each time the variable is accessed, it accesses the object stored in the memory location and returns the associated value. The said concept is illustrated in the figure below:

Python same variable name in different functions

The table represents computer memory, and the variable “x” points to the integer object stored in memory.

The above process can be verified using the id() method in Python. The id() method returns the address of the stored object that a variable points to. If the variable is assigned a different value, it will change the address to update the current value. It illustrates the concept of Python storing the integer and the variable acting more like a label. To demonstrate the concept, try the experiment below on your computer:

Python same variable name in different functions

We can see that even though x and y are different variables, they share the same address because they point to, or are labels for, the same integer value 4. What would happen when we assign a different value to the variable x?

Python same variable name in different functions

The address changes to reflect that the variable x now points to a different object with the value 5. The address displayed is that of the integer object with the value 5.

Variables can also be defined by referencing other variables. The following statement defines the variable x2 as being equal to x2. If we compare the id of both x1 and x2, we see that they are identical. It can be verified by using the “is” operator.

The test will check if two variables point to the same object. The test’s output can be seen below. The variable x2 can be thought of as just another label for the object that the variable x1 refers to.

Python same variable name in different functions

Learn more about Python variables and other basic Python concepts through CFI’s Python Fundamentals course. To keep learning, the following resources will be helpful:

  • How to Scrape Stock Data with Python?
  • Transitioning from Excel to Python
  • VBA Variable Types
  • Variability

Can you use the same variable name in different functions Python?

When a variable is defined inside a function, it is possible for a variable with the same name to refer to two different values depending on the active scope of the program. For example, the variable x contains the value 5 inside of the function. However, outside of the function, it takes the value 4.

Can you reuse variable names in different functions?

Therefore, in your case, the variables will be passed from the main driver to the individual functions by reference. So, yes, you can have variables of the same name in different scopes.

Is it OK to give the same name to variables in different methods?

In a word, yes. Variable names only hold in the scope they're defined in, and you can use the same name in different scopes.

Can Python have two functions same name?

Python supports both function and operator overloading. In function overloading, we can use the same name for many Python functions but with the different number or types of parameters. With operator overloading, we are able to change the meaning of a Python operator within the scope of a class.