Python language basics 72: object initializers and class level properties
December 13, 2015 1 Comment
We continued to explore the notion of classes in the previous post. We looked at the difference between constructors and initializers although we haven’t seen the initialisers in action yet. We also built our first class called Person that had one class method called shout. The shout method could be called on an instance of the Person class, i.e. a Person object using the standard dot notation.
In this post we’ll look at the role of initializers and how to supply method arguments to class methods.
Do you recall the difference between constructors and initializers?
- A constructor is used to construct a brand new object instance from a class
- An initializer will configure an already existing object
We’ve seen how the Person object is constructed:
person = Person()
An object initializer becomes important if you want to provide arguments to the object being constructed. To be exact the object has already been constructed, you just want to define some of its properties. Let’s say that we want to provide the person’s name, i.e. the Person class will have a property called name. How can we define that?
If you’re coming from the world of Java or C# then you’ll automatically think of private fields and overloaded constructors or constructors with one or more input parameters. There are no such things in Python. You can add class variables to “self”, i.e. “this” on the fly. Class level variables are customarily prepended with an underscore, i.e. “name” will be “_name”.
Let’s see what it looks like in code. We’ve already seen the funny looking __init__() method, i.e. a double underscore, followed by “init” and then a double underscore again:
class Person: def __init__(self, name): self._name = name def whats_your_name(self): return self._name def shout(self): print("HELLOOOOO")
The init method must also have “self” as its first argument just like in shout – and all class level methods. Then there’s a second argument “name” whose value will be assigned to the class level property “_name”. We didn’t have to define “_name” before, we just added it to “self” on the fly. Later on in the method “what’s your name” we can simply return the person’s name.
Recall this piece of code from the previous post:
person = Person() person.shout()
If you try to run it now then you’ll get an error:
TypeError: __init__() missing 1 required positional argument: ‘name’
That’s right, we have to provide the name of the Person now:
person = Person("Mike") print(person.whats_your_name())
This will print “Mike” on the screen.
What if you still want to be able to define a Person object without a name? You can add another init method which sets the person’s name to some default value like here:
class Person: def __init__(self, name): self._name = name def __init__(self): self._name = "Unknown"
You can now call…
person = Person() print(person.whats_your_name())
…and it will print “Unknown”, i.e. the person’s name was set to “Unknown”.
In the next post we’ll spend some more time on class variables.
Read all Python-related posts on this blog here.