You are on page 1of 1

Class:A Class defines the structure, property, and behaviour of an Object. You can think of a Class as a blueprint.

Exactly like building a house,


you use a blueprint to create a particular model, maybe a 3 bedroom detached dwelling. You would have a different blueprint for a 5 bedroom
dwelling for example. Its the same with Classes in their simplest form, you could define a Class representing a 3 bedroom dwelling, and another
Class that represents a 5 bedroom dwelling Object:An Object is created from a Class, like a house is created from a blueprint. You could
use a blueprint of a 3 bedroom home to build multiple 3 bedroom homes - 1 blueprint to create multiple 3 bedroom homes. This is what
happens with a Class. You can create multiple Objects from your single Class - remember, a Class is just the blueprint for creating Objects of the
same type. Instance:So we've taken our blueprint (Class), and created multiple 3 bedroom homes (Objects), but how do we refer to one
particular home (Object) we've built? We refer to a particular Object as an Instance.If I were to paint the front door of one of the homes, I
would be painting the door of a particular.Instance of the Objects (home) created from my Class (blueprint ).

Advantages of Using Classes :•Classes provide an easy way of keeping the data members and methods together in one place which helps in
keeping the program more organized.•Using classes also provides another functionality of this object-oriented programming paradigm, that is,
inheritance.•Classes also help in overriding any standard operator.•Using classes provides the ability to reuse the code which makes the
program more efficient.•Grouping related functions and keeping them in one place (inside a class) provides a clean structure to the code which
increases the readability of the program

Defining a Class in Python:• Just as a function in Python is defined using the def keyword, a class in Python is also defined using the class
keyword, followed by the class name.• Example: class Student:

Creating an Object:• As soon as the class is created, a class object is also created which is used to access the attributes in the class. The class
object have the same name as the class is used to access aftributes•Example : objectl=Student()

Sample program:

class Student:

def Greetings(self):

print("Hello !")

objectI=Student ()

objectl.Greetings()

We must notice that we are using a parameter named self while defining the function in the class, but we're not really passing any value while
calling the function. That is because, when a function is called using an object, the object itself passed automatically to the function as an
argument, so object.functionI0 is equivalent objectl.function!(objectl). That's why the very first argument in the function must be the object
itself, which is conventionally called self. It can be named something else too, but naming it 'self is a convention and it is considered as a good
practice to follow this convention

HOW TO CREATE CLASS IN PYTHON:

You might also like