how to Creating Instance Object in python | what is object in python and how access attributes and method


how to Creating Instance Object in python  | what is object in python and how access attributes and method 

Creating Instance Object:

Class provides the blueprints of an object so basically an object is created from class.

An object is block of memory that contains space to store all data member or instance variable.





how to Creating Instance Object in python . | what is object in python and how access attributes and method .

  Creating Instance Object





To create an object the following syntax is use in python-


        Syntax ->   object_name =  Class_name (arguments)



  Example :- 




Class Demo :

        var = “hello”  # variable declaration

Obj=Demo()  # object created

                                                                                                                                                               



Here Demo is a class name . obj is an Object name.






Accessing Object Variables:-


To access the variable inside of the class use object  and  dote (.) operator . that is dote operator along with object is use to access variable .


Syntax ->
                           Object_name.Variable_name




Example –


Class Demo:

           var = “hello”

  obj.Demo()

  print(obj.var) # accessing variable and print it .







Accessing Object Functions:-


To access a function inside of an abject you use notation similar to 
accessing a variable .



Syntax ->

                           Object_name. Function_name()




Example -



Class Demo:

           X= 5

           def function(self):

                  Print(“ Welcome ”)

 obj=Demo()      # object created

  Print(obj.X)     # accessing variable and print it .

 Print(obj.function())   # accessing function and print it 



Note - ( # ) symbol indicate single line comment

 (single line comment written by begin a hash (#) symbol .)








Post a Comment

Post a Comment (0)

Previous Post Next Post