Abstract
This chapter introduces object-oriented programming in Python, in the meaning of designing and implementing class hierarchies. Subclasses inherit data and functionality from superclasses and tailor data structures and methods to the needs in the subclasses. We present several examples on how to utilize object-oriented programming for numerical computing. There is also a more comprehensive example on object-oriented implementation of a drawing program, where recursive traversal of tree structures is illustrated.
Access this chapter
Tax calculation will be finalised at checkout
Purchases are for personal use only
Notes
- 1.
Two of the most widely used computer languages today are Java and C#. Both of them force programs to be written in an object-oriented style.
- 2.
This means that even the definition of a class, i.e., the class code, is an object that can be referred to by a variable.
- 3.
Observe that we carefully ensure that the divisions in methods df and ddf can never be integer divisions.
- 4.
We have placed all the classes in the file Diff.py such that these classes constitute a module. In an interactive session or a small program, we must import the differentiation classes from the Diff module.
- 5.
Strictly speaking, it is the fraction of the work and the accuracy that counts: Central4 needs four function evaluations, while Central2 and Forward1 only needs two.
- 6.
The redirection of standard input from a file does not work in IPython so we are in this case forced to run the program in a terminal window.
- 7.
This command with redirection from file must be run from a standard terminal window, not in an interactive IPython session.
- 8.
This function is discussed more in detail in Exercise 5.40.
- 9.
It is usually not a technical problem, but more a conceptual problem when the world is modeled by objects in a program.
- 10.
This guideline is good if the method converges and round-off errors do not influence the values of E i . For very large/small n, the computation of r may be unreliable.
Author information
Authors and Affiliations
Rights and permissions
Copyright information
© 2012 Springer-Verlag Berlin Heidelberg
About this chapter
Cite this chapter
Langtangen, H.P. (2012). Object-Oriented Programming. In: A Primer on Scientific Programming with Python. Texts in Computational Science and Engineering, vol 6. Springer, Berlin, Heidelberg. https://doi.org/10.1007/978-3-642-30293-0_9
Download citation
DOI: https://doi.org/10.1007/978-3-642-30293-0_9
Publisher Name: Springer, Berlin, Heidelberg
Print ISBN: 978-3-642-30292-3
Online ISBN: 978-3-642-30293-0
eBook Packages: Mathematics and StatisticsMathematics and Statistics (R0)