Template error messages may be notorious, but are by no means always long and unreadable. In this case, the entire error message (from gcc) is:
test.cpp: In function ‘void dosomething(T&) [with T = X]’:
test.cpp:11: instantiated from here
test.cpp:6: error: no match for ‘operator+=’ in ‘x += 5’
As in your Python example, you get a "stack trace" of template instantiation points, and a clear error message indicating the problem.
Sometimes, template-related error messages can get much longer, for various reasons:
- The "stack trace" might be much deeper
- The type names might be much longer, as templates are instantiated with other template instantiations as their arguments, and displayed with all their namespace qualifiers
- When overload resolution fails, the error message might contain a list of candidate overloads (which might each contain some very long type names)
- The same error may be reported many times, if an invalid template is instantiated in many places
The main difference from Python is the static type system, leading to the necessity of including the (sometimes long) type names in the error message. Without them, it would sometimes be very difficult to diagnose why the overload resolution failed. With them, your challenge is no longer to guess where the problem is, but to decipher the hieroglyphics that tell you where it is.
Also, checking at runtime means that the program will stop on the first error it encounters, only displaying a single message. A compiler might display all the errors it encounters, until it gives up; at least in C++, it should not stop on the first error in the file, since that may be a consequence of a later error.
Here is some code of mine without the TypeError: __call__() missing 1 required positional argument: 'self'
. How do you create this error?
>>> class X(Exception):
def __call__(self):
raise self
>>> x = X()
>>> x()
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
x()
File "<pyshell#3>", line 3, in __call__
raise self
X
>>> class Other(X):
pass
>>> o = Other()
>>> o()
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
o()
File "<pyshell#3>", line 3, in __call__
raise self
Other
Best Answer
The math module is a builtin, so short of modifying the Python interpreter itself, I don't think you can modify it. However, writing a module is definitely something you can do.
If you structure your files like this:
...you could simply do
import mymath
insidemyprogram.py
, and use any functions or classes insidemymath.py
as normal.So if
mymath.py
looks like this:You could do the below inside
myprogram.py
If you want the module you've written available for any program, you could either copy-and-paste it into the folder of any project you're working on, or add it to your PATH. (For example, you could include
mymath.py
inside the site-packages folder, which is located atC:\Python27\Lib\site-packages
on my computer). Once you do that, you should be able to doimport mymath
without ever having to copy-and-paste anything.As a side-note, numpy has a pretty comprehensive set of math and science related functions that you could check out. It's pretty much the de-facto standard for numerical computation in Python, afaik.