1

For below program, How could I detect the variable TypeError previously other than truly running it? Does Pylint or pyflake8 has this feature?

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

A = 1
B = 'b'
print(A+B)
leafonsword
  • 2,735
  • 3
  • 16
  • 20
  • 2
    `TypeError` is an exception that occurs at runtime. – Code-Apprentice Jan 15 '18 at 06:50
  • 1
    google 'python type hints'. this is the relevant [PEP](https://www.python.org/dev/peps/pep-0484/) and a related question: https://stackoverflow.com/questions/32557920/what-are-type-hints-in-python-3-5 . also have a look at http://mypy-lang.org/ . – hiro protagonist Jan 15 '18 at 06:51

2 Answers2

2

You can use either type or isinstance method

>>> type(1)
<type 'int'>
>>>

>>> isinstance(1, int)
True
>>>
Rakesh
  • 81,458
  • 17
  • 76
  • 113
0

TypeError is an exception that occurs at runtime. To avoid the exception, you can use type() or isinstance(). Although, if you find yourself doing this frequently, then you should rethink your code design.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268