Reputation: 41
Currently I am dealing with a lot of code inspection in pycharm. Often I have the problem that I don't know which is the actual data type of a variable in python. Then I have to revisit the code to find the actual definition of the variable, or use some inserted code snippets like type()
to get the data format.
What would be the most elegant / most comfortable way to do this task? I though maybe there are some built-in functions in PyCharm.
Upvotes: 2
Views: 3860
Reputation: 692
You can see "Inferred type". Sometimes, as in the case above, it is not what the real variable ever be in this place, but at least PyCharm tried it best to infer.
import random
variable = 1
variable = None
for i in range(10):
variable = random.randint(1, 3)
if variable <= 0:
variable = 'Zero or less'
elif variable == 1:
variable = 1.0
elif variable == 2:
variable = [2]
elif variable == 3:
variable = {'three': 3}
else:
variable = (4, '4 or more')
print(i, type(variable), variable)
# Extra blank lines to show variable type
#
#
#
del variable
Upvotes: 1
Reputation: 1728
Welcome to stackoverflow. You can always try to have a look at the Quick documentation to get a documentation hint in the Jetbrains IDEs.
But since Python is dynamically typed this might not help you every time if you are not using Python type hints and the code is getting "complicated". There is a shortcut to look at the definition of a symbol/variable which will allow you to remember what type you intented to use.
You have to be aware though that you can change what you bind to a variable, meaning that at the beginning foo
binds an integer (foo = 0
) and later you change it to a string (foo = ""
). This is totally fine is Python and therefore the code inspection of PyCharm might give you wrong information.
Upvotes: 2