<\/path>\n<\/svg><\/div><\/div>How Do You Define a Class in Python?<\/h1> In Python, you define a class by typing the class keyword, followed by a name and a colon. Then, you use.init<\/strong>() to declare what attributes each instance of the class should have:<\/p><\/circle><\/circle><\/circle><\/g><\/svg><\/span><\/path><\/path><\/svg><\/span>class<\/span> <\/span>Student<\/span>:<\/span><\/span>\n <\/span>def<\/span> <\/span>__init__<\/span>(<\/span>self<\/span>, <\/span>name<\/span>, <\/span>age<\/span>):<\/span><\/span>\n <\/span>self<\/span>.student_name<\/span>=<\/span> name<\/span><\/span>\n <\/span>self<\/span>.student_age <\/span>=<\/span> age<\/span><\/span><\/code><\/pre><\/div>This code defines a class called Student<\/code> that has two attributes: student_name<\/code> and student_age<\/code>.<\/p>Key Points<\/h3>Class Definition<\/strong>: class Student:<\/code> starts the definition of the Student<\/code> class.<\/li>\n\nConstructor Method<\/strong>: def __init__(self, name, age):<\/code> is a special method called the constructor. It is automatically called when you create a new instance of the class.<\/li>\n\nAttributes<\/strong>: self.student_name<\/code> and self.student_age<\/code> are the attributes of the class. self<\/code> refers to the instance of the class being created. name<\/code> and age<\/code> are parameters that you provide when creating a new Student<\/code> object.<\/li>\n\n<\/li><\/ul>Example Usage<\/strong><\/h2><\/circle><\/circle><\/circle><\/g><\/svg><\/span><\/path><\/path><\/svg><\/span>student1 <\/span>=<\/span> Student(<\/span>"<\/span>Alice<\/span>"<\/span>, <\/span>20<\/span>)<\/span><\/span>\nprint<\/span>(student1.student_name) <\/span># Output: Alice<\/span><\/span>\nprint<\/span>(student1.student_age) <\/span># Output: 20<\/span><\/span><\/code><\/pre><\/div>In this example, student1<\/code> is an instance of the Student<\/code> class with student_name<\/code> set to “Alice” and student_age<\/code> set to 20.<\/p>\n <\/path>\n<\/svg><\/div><\/div>Why Class Used<\/strong><\/h1>But what does all of this mean? And why would you need classes in the first place? Take a step back and consider using built-in primitive data structures<\/mark> instead.<\/p>Primitive data structures\u2014such as numbers, strings, and lists<\/mark>\u2014are intended to represent simple pieces of information, such as the price of an apple, the title of a poem, or your favorite colors. What if you want to depict something more complex?<\/p>For example, you may want to keep track of Students in an collage. You should keep some basic information about each students , such as their name, age, branch, and the year they began admission.<\/p>
One way to accomplish this is to represent each students as a list.<\/p>
<\/circle><\/circle><\/circle><\/g><\/svg><\/span><\/path><\/path><\/svg><\/span>science<\/span>=<\/span> [<\/span>"<\/span>gp<\/span>"<\/span>, <\/span>17<\/span>, <\/span>"<\/span>science<\/span>"<\/span>, <\/span>2265<\/span>]<\/span><\/span>\ncommerce<\/span>=<\/span> [<\/span>"<\/span>jna<\/span>"<\/span>, <\/span>18<\/span>, <\/span>"<\/span>Commerce<\/span>"<\/span>, <\/span>2254<\/span>]<\/span><\/span>\narts<\/span>=<\/span> [<\/span>"<\/span>pm<\/span>"<\/span>, <\/span>"<\/span>Arts<\/span>"<\/span>, <\/span>2266<\/span>]<\/span><\/span><\/code><\/pre><\/div>There are several issues with this approach.<\/p>
First, larger code files may become more difficult to manage. Will you remember the students name if you refer to science[0] several lines after declaring the science list?<\/p>
Second, errors can occur if students’ lists do not contain the same number of elements. The age is missing from the arts list above, so arts[1] returns “Arts” rather than pm age.<\/p>
Classes are an excellent way to make this type of code easier to manage and maintain.<\/p>
\n <\/path>\n<\/svg><\/div><\/div>Classes vs Instances<\/h1> Classes enable you to create user-defined data structures. Classes define functions known as methods, which specify the behaviors and actions that an object derived from the class can perform with its data.<\/p>
In this tutorial, you’ll create a Dog class that contains information about the characteristics and behaviors that a specific dog can exhibit.<\/p>
A class is a template for how to define something. It does not actually contain any data. The Dog class states that a name and age are required for defining a dog, but it does not include the name or age of any particular dog.<\/p>
The class is the blueprint, whereas an instance is an object created from a class that contains real data. An instance of the Dog class is no longer a blueprint. It is a real dog with a name, such as Miles, who is four years old.<\/p>
In other words, a class functions similarly to a form or questionnaire. An instance is similar to a form that you’ve filled out with information. Just as many people can fill out the same form with their own unique information, you can create multiple instances from a single class.<\/p>
\n <\/path>\n<\/svg><\/div><\/div>Class Definition<\/h1> You start all class definitions with the class<\/code> keyword, then add the name of the class and a colon. Python will consider any code that you indent below the class definition as part of the class\u2019s body.<\/p>Here\u2019s an example of a Car class:<\/p>
<\/circle><\/circle><\/circle><\/g><\/svg><\/span><\/path><\/path><\/svg><\/span># car.py<\/span><\/span>\n<\/span>\nclass<\/span> <\/span>Car<\/span>:<\/span><\/span>\n <\/span>pass<\/span><\/span><\/code><\/pre><\/div>The Car class’s body contains only one statement: the pass keyword. Python programmers frequently use pass as a placeholder for where the code will eventually go. It allows you to run this code without encountering a Python error.<\/p>
Note:<\/strong> Python class names are written in CapitalizedWords notation<\/a> by convention. For example, a class for a specific type of car, like the Tesla Electric Car, would be written as TeslaElectricCar.<\/p>The Car class isn’t particularly interesting right now, so you’ll spice it up by specifying some properties that all Car objects should have. There are several properties to choose from, including name, model, year and color. To keep the example simple, you will only use name and model.<\/p>
You define the properties that all Car objects must have in a method called init<\/strong>(). Every time you create a new Car object init<\/strong>() determines the object’s initial state by assigning values to its properties. That is init<\/strong>() initializes each new instance of the class.<\/p>You can pass any number of parameters to init<\/strong>(), but the first parameter is always a variable named self. When you create a new class instance, Python automatically passes it to the self parameter in init<\/strong>(), allowing Python to define the object’s new attributes.<\/p>Update the Car class with a init<\/strong>() method that generates the.name and Model attributes:<\/p><\/circle><\/circle><\/circle><\/g><\/svg><\/span><\/path><\/path><\/svg><\/span># car.py<\/span><\/span>\n<\/span>\nclass<\/span> <\/span>Car<\/span>:<\/span><\/span>\n <\/span>def<\/span> <\/span>__init__<\/span>