Welcome back, Class Recap
February 7, 2012 5 Comments
Happy New Year
Welcome back to Python4Kids! I am sorry for being a little slack since the end of last year, but hopefully I’m over that now and we can start powering on again!
Towards the end of last year we started working on classes and a GUI toolkit called Tkinter. In this tutorial we will recap classes. Hopefully, we’ll recap Tkinter next. If you have data and functions which are related to each other in some way, classes allow you to group them together. This makes managing them easier, particularly as your programs get bigger.
>>> class allDads(object): ... def __init__(self,age=28): ... self.age = age ... >>> dad1 = allDads() >>> dad1.age 28 >>> dad2 = allDads(35) >>> dad2.age 35 >>> dad1.age 28
This class has one method (called __init__()) and one attribute (called age). It inherits from object.
The main things we have learned about classes are:
- they are based on (“inherit from”) some Python object (usually object <- the italics here refers to the Python thing called object as opposed to object in its ordinary sense)
- classes are defined with the class statement. The class statement creates an archetype* – that is, a kind of template – from which specific instances are created. Thus the class allDads was representing all dads in the world, while myDad, was a specific instance of a dad – that is, my dad in particular.
- data which is stored in a class or instance is called an attribute.
- functions which are part of a class or instance are called methods
- if you have an instance of a class (eg myDad), then the attributes and methods of myDad are identified by the dot operator “.”. So, the appearance attribute of the instance myDad is identified by myDad.appearance (see the dot?). If it had a method called makesARobot() (the parenthesis indicates it is a function or method) that would be identified by myDad.makesARobot().
- the attributes of an instance are (typically) specific to that instance. If two instances have an attribute of the same name (which will always be the case for attributes defined by the class), changing the attribute for an instance does not affect the attribute of another instance.
- classes usually define a special method called __init__() (“dunder init“, among other pronunciations). This method is run whenever an instance of the class is created. It INITialises the instance.
- classes make use of special variable called self. self is a bit tricky to explain, but it will become clear when you start using it a bit. Self is used when defining the class as a way for each instance to refer to that instance of the class, rather than to the class as a whole.
Python’s classes are a core component of the language. You will end up using them all the time. In fact, they are also a core part of object oriented programming. I have introduced classes at the same time as Tkinter because Tkinter needs them. In order to program Tkinter, you basically need to use classes!
Create a class with a method called __init__(self, firstName = None). Make the __init__ method assign the value of the firstName parameter to an attribute which is also called firstName. Hint: check the example above, and use self.firstName).
Create some instances of your class, passing your first name as a string (ie put it in quotation marks) to the class.
Print the firstName attribute of the instances you have created.