Get startedGet started for free

Class anatomy: attributes and methods

1. Class anatomy: attributes and methods

Great job so far! Now that we know how to work with existing objects and classes, let's look at how to create our own!

2. A Customer class

To start a new class definition, we start with a class statement, followed by the name of the class, and end with a colon. We then indent code beneath. Everything in the indented block after will be considered a part of the class. We 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, c_one and c_two are two different objects of the empty class Customer. We want to create objects that store data and can perform defined tasks - in other words, have attributes and methods.

3. Add methods to a class

Here, we define the "identify" method for the Customer class. Recall that methods are functions, so the definition of a method looks just like a regular Python function, with one exception. We provide the special self argument that every method will have as the first argument, followed by name as a second argument. The method will print "I am Customer" plus name when called. We'll get back to self in a minute, first, let's see how this works. We create a new customer object by calling the name of the class, then call cust.identify passing 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 in the method call.

4. What is self?

So what was that self? Classes are templates. So when we make the class there are not yet any associated objects. So, we include self as the first argument to methods as a way of referring to the object that we will create. Put another way, it's a stand-in for the future object. When a method is called from an object, it is the equivalent of calling the method from the class and passing the object, so cust.identify is the same as Customer.identify, where cust is passed as the first argument. That's why we don't specify it explicitly when calling the method from an existing object.

5. We need attributes

What about data, or state? In OOP, we include this within methods, therefore bundling state and behavior. 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 we need to assign a value to an attribute.

6. Add an attribute to class

Here is a method set_name with arguments self and new_name. To create an attribute of the Customer class called "name", we need to assign something to self-dot-name. Remember, self is a stand-in for an object, so self-dot-attribute is equivalent to 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 is 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, we can improve our identify method! We add the set_name method we just discussed. Now, in the identify method we remove the name argument and refer to the self.name attribute, which, will pull the name attribute from the object that called the method. We create a new customer object, call the set_name method and provide our name. Now, the identify method will only use the data that is associated with the object, instead of using whatever we passed to it.

8. Let's practice!

Now, let's start creating our own classes!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.