When a variable is referred in documetation string, ``
is used:
def func(var):
"""
Parameter ``var`` is referred in a documetation string.
"""
What is the convention for printing a variable name? (or using in a string to be shown to the user, such as exception's message) Would it be the same:
def func(var):
if not check1(var):
raise Exception("parameter ``var`` has to be...")
if not check2(var):
logger.warn("parameter ``var`` is...")
Let's just generate some exceptions in the interpreter:
Python 3.5.2 (default, Sep 14 2017, 22:51:06)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> sdf
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'sdf' is not defined
>>> mystr = 'sdf'
>>> mystr.foo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'foo'
As we can see, a common practice for exception messages seems to be single quotes, as in 'name'
:
NameError: name 'sdf' is not defined
AttributeError: 'str' object has no attribute 'foo'
Even better example by the OP (@Itay):
>>> def a(d):
... pass
...
>>> a()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: a() missing 1 required positional argument: 'd'