"Everything in Python is an object" is a good way of getting to know the language. If you would upgrade your Python programming skills, you need to think in terms of objects.
A good advice I received from a Pythonista is that whenever you have to create global variables in your program, create objects instead.
class
You define a class and create objects from it. A class is like a seal or a rubber stamp if you like. The seal or the stamp is designed once and applied several times.
A class has a specific responsibility in a program. It is designed to hold data and operations on that data. Data is held by variables in the class and operations on the data are performed by methods defined in the class. The responsibility of a class depends on the kind of object it is modelled after.
In plain English, we say, 'a class of animals' when referring to a species. Or, a class of books, when referring to a genre of literature. When we want to model, say, animal species or literary fiction, we create a class in our program. Any object in the world around us can be modelled as a class in Python.
Class definition
A class typically has attributes and methods. Attributes are variables that hold the data and methods are operations to perform on that data.
Let us model a class of animals.
class NamedAnimal:
# object constructor
def __init__(self, name, kind):
self.name = name
self.kind = kind
# display object info
def __repr__(self):
return f'{self.name} is a {self.kind} animal'
# create an object called fish
fish = NamedAnimal('Tuna', 'water')
print(fish)
The object constructor is the init() method. It is a special method, also called magical or dunder method. Note that it has
- leading double underscores
- trailing double underscores
- a parameter called self
- two parameters called attributes
Dunder methods
The dunder methods are called by the Python interpreter. We have two dunders in our class, one for object creation and the other for displaying the attributes as text. We have used the f-string to print the class data. It is returned by the repr() method. This function used str() dunder internally, so basically it is a string representation.
self
self refers to the object in question. The variables hold the class characteristics, also called attributes. All such variables are bound to the self .
Inside the class, the instance variables are accessed using self; however, outside the class the instance variables are accessed using the object or instance of the class.
Class instances
An object is constructed by calling the name of the class as if it were a method with parameters. However, the init() is called by the Python interpreter at runtime.
fish = NamedAnimal('Tuna', 'water')
Objects are instances of a class. Objects live in the program's memory. Objects are created and used and later removed from the memory heap after use.
The variable fish is an object of the class NamedAnimal, also called an instance of the class.
Accessor & Mutator
Let us add a couple of methods to the class. These methods allow us to modify the attributes and access their values. They are
- accessors, also called getters
- mutators, also called setters
Update the class with the following code snippet.
# mutator (setter)
def setAnimalProperties(self, name, kind):
self.name = name
self.kind = kind
# accessor (getter), returns a tuple
def getAnimalProperties(self):
return (self.name, self.kind)
We have added two methods, an accessor and a mutator. We can modify the class attributes after it has been created. Add the following code block at the end. The properties of the fish object have been modified using the methods, like so.
name, kind = fish.getAnimalProperties()
print(name, kind)
fish.setAnimalProperties('Nemo', 'water')
print(fish)
Be careful how you copy and paste the code. If the indentation is not proper, it will fail to run. Note that all methods, including the constructor, have the mandatory self .
Exercise
- Add appropriate methods to model some characteristics of the animals, depending on whether they are land, water or air animals.
- Create a bunch of animal objects of different kinds and store them in a list. Print the list.