Overview
Python is an object-oriented programming language. It allows combining data and functionality. Essentially everything in Python is an object with attributes and methods. A class defines an object's structure and behavior.
Introduction
In Python, classes are utilized to characterize new types of objects. Classes allow encapsulating data and the functions that work on that data into a single entity. This way, classes give a way to organize code and simplify it to preserve, reuse, and amplify.
Creating a Class in Python
In Python, creating a class is as simple as writing a function. Function declarations begin with the term def, but class definitions begin with the keyword class . After the keyword class , we have the class identifier (the name of the class we defined), followed by the: (colon) operator. In addition, the variables within the class are referred to as attributes. With the dot(.) operator 📍, the characteristics may be retrieved later in the program.
Loading...
Example
Loading...
It should be noted that the term pass implies an empty class. It is just written to prevent problems in the console while running the preceding code.
Python Class Attributes and Methods
To fully utilize Python classes, we must also add functionality to them. We can do this with attributes and methods. Methods are the functions we define within a class. We need to define specific attributes that will hold functions and data. This will enhance our code's performance. Let's briefly explore these additional features.
Python Class Attributes
class attributes are variables that are shared over all instances of a class. They represent characteristics of the class as a whole and are defined outside of any of the class methods. For illustration, in a student class, a class attribute could be the number of students within the class. This attribute would be shared between all instances of the class and might be accessed by any instance.
Loading...
The class attribute in this example is num_students. This attribute is shared between all class instances and increments each time a new instance is created. Each class instance can access this attribute to determine the total number of students in the class.
Python Class Methods
Class methods are functions that are defined and associated with a class. They are used to perform operations on the class's data. Class methods are invoked using the class name and not an instance of the class. They are used to perform tasks related to the class itself, such as creating a new class instance, initializing class properties, and more.
Loading...
Example
Loading...
The code defines a class called Person, which has an init() method to initialize the name attribute of an instance. It also includes a class method called create_person(), which takes a name as an argument and returns an instance of the Person class with the given name. The create_person() method is invoked utilizing the class name, Person, and not an instance of the class.
Self keyword
In Python, the self keyword is utilized to allude to the class instance. It is crucial for accessing the attributes and methods of the class. The self keyword is used as the first argument at whatever point a method is called. The self keyword binds the class's attributes to the specific instance calling the method at a given time. In other words, it empowers the class's attributes to be gotten to in a particular instance. With the self keyword, it would be clear which instance of the class the attributes and methods belong to. Overall, the self keyword is crucial for establishing the link between an instance of a class and its corresponding attributes and methods. It allows instances to access and modify their attributes dynamically. The self keyword is an integral part of object-oriented programming in Python and efficiently utilizes classes and instances.
Example:
Loading...
In the code above, the self keyword refers to the class instance. It is used to access the class's attributes (name and age). Inside the print_details() method, the self keyword is used to access the name and age attributes of the instance.
Instance Attributes (_init_method) in Python:
Instance attributes are variables owned by the specific instances of a class. They are defined inside the init method of a class and available to each class instance. They are different from class attributes, which the class owns. Instance attributes are used to store information that is unique to each instance.
Loading...
In this example, we have defined a Person class with two instance attributes, name, and age. The init method of the Person class takes two parameters (name and age) and sets them as instance attributes on the instance object. We then create two instances of the Person class, p1 and p2, and set the name and age attributes for each instance. Finally, we print out the name and age attributes for each instance, which results in the output "John", "36", "Sarah", and "28".
Python Class Properties
Class properties are variables that are associated with a specific class . These variables are usually declared within the class definition and can be accessed from any class instance. Class properties can be used to store data related to the class and shared among all instances of a class. The property() function is a built-in function in Python that allows us to create, get, and set properties on a class or object. It takes three arguments: a getter function, a setter function, and a deleter function. The getter function is used to retrieve the value of a property, the setter function is used to set the value of a property, and the deleter function is used to delete the property. This allows us to create custom attributes on a class or object easily and also allows us to define custom behavior when accessing or changing the values of those attributes.
Example:
Loading...
This code makes a class named MyClass that includes a name property. It, at that point, makes a property object named name_property, which is related to the name property. It, at that point, creates getter, setter, and deleter methods for the property, which are utilized to get, set, and delete the value of the name property. At last, it sets the property object's getter, setter, and deleter methods to the already created methods. This permits us to effectively get to and modify the value of the name property from any instance of the class.
Conclusion
Python is an object-oriented programming language where everything is an object with attributes and methods. Classes define an object's structure and behavior, allowing for data and functionality to be combined. Attributes and methods add functionality to classes; the self keyword refers to the class instance. Class attributes are variables shared across all class cases, while class methods are functions associated with a class used to perform operations on the class's data.
Key takeaways
Quiz
Answer: b. To initialize the class
Answer:c. class MyClass:
Loading...
a. 5
b. 10
c. Error
d. None
Answer:a. 5
Answer:a. To access instance attributes
Top Tutorials
Related Articles