Computational costs of typechanges/reassignments in python

25 Views Asked by At

In the past, I defined most often new names for new variables in python. Nevertheless, reusing names may sometimes come with cleaner code like in this example.

With new names:

this_path = "/home/maestroglanz/private/gump"
this_path_as_list = this_path.split("/")
this_path_cut = this_path_as_list[:-2]
this_path_reassembled = "/".join(this_path_cut)
print(this_path_reassembled)

Alternative with reusing names:

this_path = "/home/maestroglanz/private/gump"
this_path = this_path.split("/")
this_path = this_path[:-2]
this_path = "/".join(this_path)
print(this_path)

Both programs result in

/home/maestroglanz

The readability of the second one is better in my opinion, but this is individual preference. What I want to know is: Which of these code snippet is computationally more expensive or are they literally identical. I googled for computational costs and haven't found a lot so far.

0

There are 0 best solutions below