Professional Documents
Culture Documents
Class
Class
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