Published online by Cambridge University Press: 05 February 2015
Creating classes
For simple tasks involving short programs, you can survive perfectly well with the standard Python data types for holding information, such as lists and dictionaries. However, for more complicated tasks involving long programs, this often becomes unwieldy. There are various ways to deal with this issue, but one of the most fruitful is the ability to define your own data types: objects built to your own specification, organised in the way that is convenient to you. Modern computer languages do this via the introduction of bespoke object definitions that are known as classes and this kind of thinking is generally termed object-oriented programming.
When creating your own custom data types, the class is the definition of a particular kind of object in terms of its component features and how it is constructed or implemented in code. The term object, however, refers to a specific instance, or occurrence, of the thing which has been made according to the class definition. The making of an object of a given class is what is usually termed instantiation. A convenient analogy is to think of the blueprint for a house being like a class, but the actual, solid house being the object. Also, given a single blueprint one may build many instances of different house objects, all to the same design. It is quite common to use the words ‘class’ and ‘object’ interchangeably, even in the same context, although they mean different things, and it is important to understand the difference. As it happens, everything that is brought into existence in Python is an object, so even integer and floating point numbers are objects, although most of the time you can work without noticing that.
To save this book to your Kindle, first ensure [email protected] is added to your Approved Personal Document E-mail List under your Personal Document Settings on the Manage Your Content and Devices page of your Amazon account. Then enter the ‘name’ part of your Kindle email address below. Find out more about saving to your Kindle.
Note you can select to save to either the @free.kindle.com or @kindle.com variations. ‘@free.kindle.com’ emails are free but can only be saved to your device when it is connected to wi-fi. ‘@kindle.com’ emails can be delivered even when you are not connected to wi-fi, but note that service fees apply.
Find out more about the Kindle Personal Document Service.
To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Dropbox.
To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Google Drive.