Skip to main content

26.Python Classes and Objects

Python Classes and Objects:
In Python, a class is considered as the definition of the object and the object is defined as the instance of a class.The programmers tend to solve the real world problems in a virtual environment made of objects and classes.This is known as Object oriented programming.Object oriented programming has always been a focus for python programming language.Languages like C are called procedural languages where as python,Java,C++ etc are object oriented languages.The procedural languages are the sequences of code to get the desired output where as the object oriented languages uses blocks of code to get the desired output.
For example,
Think of procedural language like a cook book where it consists sequences of procedure to make a cake like get a bowl,mix the floor,put it oven etc.While in the object programming ,instead of the series of code ,we tell or declare the objects we use and its functions.Say for example,we declare an object called mixer which does the function mixing,an object oven heats up anything inside it.
Now when I want to make something other than cake,say a juice,I need to write a new code in procedural programming whereas in object oriented programming I can use the existing object mixer to make a juice.Thus object oriented programming has a feature of re-usability.

Class:   
    A class is the basis of all data in python.A class is a design or blueprint of the objects.For better understanding,imagine a factory that manufactures many products.The factory is a class that helps to design the products(objects).The class definition begins with the keyword "class".We can have variables and functions within the class.Note that the first argument of the function is always self.self is a reference to an object.self is not a keyword but to enhance readability most programmers often use self as the first argument.Remember,functions within the classes are known as methods.In short,the self denotes the object or the object gets passed as a self in the method.
syntax:
class class_name:
    statements/variables
    def function_name(self):
        statements
Object:
    As mentioned earlier,object is the instance of class.The object is a real world entity.It has a well defined structure and behaviour.In order to store data in a object,variables are used.To define the behaviour of the object,methods(functions) are used.
For example,
In an office,the employee is an object.the object(employee)has certain data(knowledge) and methods(behaviour). 
Object is created by calling the class_name.class is a logical entity and object is a physical entity.
syntax:
object_name=class_name()
object_name.function_name()
Program
class Tamilhacks:#class creation
    variable1="TamilHacks"
    def message(self):
        print(self.variable1) 
object=Tamilhacks()#object creation
object.message()#accessing the method message()
Tamilhacks.message(object)#accessing the method message()-another method
Output
TamilHacks
TamilHacks
    Also,it is possible to create more than one object for a class.Remember each object is different.The object will be stored in the heap memory of a computer.Every time an object is created,a new space is allocated.The size of the object depends on the number of variables assigned.
Program
class Tamilhacks:#class creation
    variable1="TamilHacks"
    print("welcome")
    def message(self):#method1 
        print(self.variable1) 
    def Greeting(self):#method2
        print("Hello")
object=Tamilhacks()#object1 creation
object.message()#accesing the method message()
object2=Tamilhacks()#object2 creation
object2.message="learn python"
object2.greeting="Howdy!"
print(object2.message)
print(object2.greeting)
Output
welcome 
TamilHacks 
learn python 
Howdy!
To know more about python classes and objects,check out the video below!
Happy Learning!!



Comments

Popular posts from this blog

Get Ethical Hacking Project #1

Thanks for your interest in our course This is a paid course, 150 rs for Indian people, 5$ for international people Instructions to get course: Indian people can get the course by paying using this UPI id   tamilhacks@ybl  (KARTHIKEYAN T) send 150 Rs to this upi id with subject bug bounty and after payment take a screenshot and send that to email id tamilhackstech@gmail.com - you will get the course within 24 hours. (there will be no tax, when paying using upi id)     The above mentioned method is most preferable. if you don't have a upi id app, install  phonepe  or  google pay   app and register there and pay easily  or  pay here using the alternate method to get this course, this link cost 18% gst for the course . (nearly 30-100 Rs for tax and other charges here )   After payment you will receive a payment confirmation mail along with a downlink for the course, you can download the course file using that . the course is in zip format, so after downloading the file extra

Learn c++ in Tamil | Complete guide and tutorial | Beginner to Advance all concepts explained.

hi guys welcome , all the coding used in video are available here : /* hi guys welcome to learn c++ in tamil what is C++ how what software your own program schools,clg,institute.... start 1.interest 2.patience 3.fun 4.practise computer program - app - programming source code - hardware - fb running , executing machine language :  101010 assembly lang : mov h1; high lang : c , c++....java , js , python  eg :  a+b .....1001010  compiler :  human to machine              machine to human  rules :  a+b;  usage:  games, manufacturing , embedded , finance , graphics , etc...  Guide: 1.problem 2.solution - alternative 3.program  - C++ 4.compile  - turboc , ide , codeblocks , cmd 5.result   - library file - linker : #include<iostream.h>    input , output std::cout<<"welcome";   standard character out << " "; primary section : 1.statements   ;  end 2.functions   main() 3.syntax  , syntax error pra

Get Bug Bounty Course

Thanks for your interest in our course Limited Offer This is a paid course, Rs 10000 ,Rs 1000  for Indian people, 75$ 15$ for international people , 50$ 10$ for Srilankan people Instructions to get course: Indian people can get the course by paying using this UPI id  tamilhacks@ybl  send 1000 Rs to this upi id with subject bug bounty and after payment take a screenshot and send that to email id tamilhackstech@gmail.com - you will get the course within 24 hours. (there will be no tax, when paying using upi id)    The above mentioned method is most preferable. if you don't have a upi id app, install phonepe or google pay   app and register there and pay easily  or  pay here using the alternate method to get this course, this link cost 18% gst for the course . (nearly 30-100Rs for tax and other charges here ) After payment you will receive a payment confirmation mail along with a downlink for the course, you can download the course file using that. As for international peopl