1. Class anatomy: attributes and methods
Great job so far! Now that you know how to work with existing objects and classes, you'll learn how to create your own!
2. A basic class
To start a new class definition, all you need is a class statement, followed by the name of the class, followed by a colon. Everything in the indented block after will be considered a part of the class.
You can create an "empty" class -- like a blank template -- by including the pass statement after the class declaration.
Even though this class is empty, we can already create objects of the class by specifying the name of the class, followed by parentheses. Here, c1 and c2 are two different objects of the empty class Customer.
We want to create objects that actually store data and operate on it -- in other words have attributes and methods.
3. Add methods to a class
Defining a method is is simple. Methods are functions, so the definition of a method looks just like a regular Python function, with one exception: the special self argument that every method will have as the first argument, possibly followed by other arguments. We'll get back to self in a minute, first let's see how this works.
Here we defined a method "identify" for the Customer class that takes self and a name as a parameter and prints "I am Customer" plus name when called.
We create a new customer object, call the method by using object-dot-method syntax and pass the desired name, and get the output.
Note that name was the second parameter in the method definition, but it is the first parameter when the method is called. The mysterious self is not needed the method call.
4. What is self?
So what was that self?
Classes are templates. Objects of a class don't yet exist when a class is being defined, but we often need a way to refer to the data of a particular object within class definition. That is the purpose of self - it's a stand-in for the future object.
That's why every method should have the self argument -- so we could use it to access attributes and call other methods from within the class definition even when no objects were created yet.
Python will handle self when the method is called from an object using the dot syntax. In fact, using object-dot-method is equivalent to passing that object as an argument. That's why we don't specify it explicitly when calling the method from an existing object.
5. We need attributes
By the principles of OOP, the data describing the state of the object should be bundled into the object.
For example, customer name should be an attribute of a customer object, instead of a parameter passed to a method.
In Python attributes -- like variables -- are created by assignment, meaning an attribute manifests into existence only when a value is assigned to it.
6. Add an attribute to class
Here is a method set_name with arguments self (every method should have a self argument) and new_name. To create an attribute of the Customer class called "name", all we need to do is to assign something to self-dot-name. Remember, self is a stand-in for object, so self-dot-attribute should remind you of the object-dot-attribute syntax.
Here, we set the name attribute to the new_name parameter of the function.
When we create a customer, it does not yet have a name attribute. But after the set_name method was called, the name attribute is created, and we can access it through dot-name.
7. Using attributes in class definition
Equipped with the name attribute, now we can improve our identification method! Instead of passing name as a parameter, we will use the data already stored in the name attribute of the customer class.
We remove the name parameter from the identify method, and replace it with self-dot-name in the printout, which, via self, will pull the name attribute from the object that called the method. Now the identify function will only use the data that is encapsulated in the object, instead of using whatever we passed to it.
8. Let's practice!
Now, let's start creating your own classes!