Get startedGet started for free

Class vs. instance attributes

1. Class vs. instance attributes

Great work so far! Now we're going to dive deeper into classes versus objects. But first, let's examine the core principles of OOP.

2. Core principles of OOP

First, there is encapsulation, which is the term for grouping data and methods together. Second, there is inheritance, which allows us to build on top of existing code. Lastly, polymorphism enables the access of different types of objects through the same interface, meaning we can use different objects from the same class in the same way.

3. Instance-level attributes

We've actually been implementing encapsulation in our classes already by creating objects with different attribute values and using our custom-defined methods. We can extend this by separating instance and class-level data! Remember the Employee class we previously defined? It had attributes like name and salary, and we were able to assign values to them when creating each new object. These attributes are only applicable to the object we assigned them to. The use of self within the method allowed us to assign to a particular object when they are initialized.

4. Class-level attributes

But what if we need to store some data that is shared among all the instances of a class? For example, if we want to introduce a minimal salary across the entire organization. That data should not differ among objects. Then, we can define an attribute directly in the class body, rather than when we define the init constructor. This will create a class attribute, that will serve as a "global variable" within a class. For example,

5. Implementing class-level attributes

we can define the MIN_SALARY attribute and set it to 30000. We don't use self to create this attribute. The convention is to capitalize class attributes. We can use this attribute inside the class like we would use any global variable, only we use class name-dot-attribute syntax rather than self. For example, here, we check if the value of the salary attribute is greater than MIN_SALARY in the init method. If so, we assign the salary value to self, otherwise, we assign the MIN_SALARY value.

6. Class-level attributes

This min_salary variable will be shared among all the instances of the Employee class. We can access it like any other attribute from an object instance, and the value will be the same across instances. Here, we print the MIN_SALARY class attribute from two employee objects. This approach is an example of encapsulation, where data and methods are grouped within the class.

7. Modifying class-level attributes

Remember, MIN_SALARY is a class-level attribute. So what would happen if we change its value for a specific object? We have two employee objects and set the min_salary of the first one to 50000. When we print the min_salary of both employees, we see that it has only changed for the first one.

8. Modifying class-level attributes

The min_salary attribute is created in the class definition, so updating this attribute's value for an individual object will not change the value in the class! This makes sense from a security perspective - we don't want a scenario where changing data in a single instance results in changes to data in all other instances from a class!

9. Why use class attributes?

Now we understand the scope of class attributes, let's discuss why they are useful! Firstly, they can be used to set global constants for a class, such as minimum and maximum values for attributes, like min_salary, serving as a safety measure to prevent invalid data. Secondly, they can be used for common values like configuration settings for a Database class, avoiding repetition when creating objects.

10. Let's practice!

That was a lot! Now head over to the exercises to practice.

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.