Skip to main content

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

  • 14k Accesses

Abstract

A class packs a set of data (variables) together with a set of functions operating on the data. This allows the programmer to compose new objects with content and behavior designed for the problem at hand. Most of the mathematical computations in this book can easily be coded without using classes, but classes often offer more elegant solutions or code that is easier to extend at a later stage. This chapter gives an introduction to the class concept in Python with emphasis on applications to numerical computing. We cover in particular constructions that allow user-designed objects to enter arithmetic expressions.

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.

    As long as we are on the surface of the earth, g can be considered fixed, but in general g depends on the distance to the center of the earth.

  2. 2.

    All familiar Python objects, like lists, tuples, strings, floating-point numbers, integers, etc., are in fact built-in Python classes, with names list, tuple, str, float, int, etc.

  3. 3.

    The name can be any valid variable name, but the name self is a widely established convention in Python.

  4. 4.

    The comma-separated list of variables on the right-hand side forms a tuple so this assignment is just the usual construction where a set of variables on the left-hand side is set equal to a list or tuple on the right-hand side, element by element. See page 57.

  5. 5.

    The instance can also contain static class attributes (Chapter 7.6), but these are to be viewed as global variables in the present context.

  6. 6.

    Newton’s method converges very slowly when the derivative of f is zero at the roots of f. Even slower convergence appears when higher-order derivatives also are zero, like in this example. Notice that the error in x is much larger than the error in the equation (epsilon).

  7. 7.

    Technically, it is possible to grab the coeff variable in a class instance and alter this list. By starting coeff with an underscore, a Python programming convention tells programmers that this variable is for internal use in the class only, and not to be altered by users of the instance, see Chapters 7.2.1 and 7.5.2.

  8. 8.

    This may sound scary and highly illegal to C, C++, Java, and C# programmers, but it is natural and legal in many other languages – and sometimes even useful.

  9. 9.

    The formula arises from the solution of Exercise 1.13 when v 0=0.

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). Introduction to Classes. 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_7

Download citation

Publish with us

Policies and ethics