Skip to main content

Part of the book series: Texts in Computational Science and Engineering ((TCSE,volume 6))

  • 14k Accesses

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.

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD 59.99
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Notes

  1. 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. 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. 3.

    Observe that we carefully ensure that the divisions in methods df and ddf can never be integer divisions.

  4. 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. 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. 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. 7.

    This command with redirection from file must be run from a standard terminal window, not in an interactive IPython session.

  8. 8.

    This function is discussed more in detail in Exercise 5.40.

  9. 9.

    It is usually not a technical problem, but more a conceptual problem when the world is modeled by objects in a program.

  10. 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

Authors

Rights and permissions

Reprints 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

Publish with us

Policies and ethics