Saturday, August 21, 2021

Day3 - Object Oriented Programming in Python

 Day3 - Object Oriented Programming in Python 
8/21/2021

What is behavior?
-> Behavior represent functionality (functionality is a logic)
We can define following behavior in python
1. Constructor
2. Method

What is method?
- a behavior which represent some functionality will execute whenever programmer will call is called method
for eg, c1.m1 or c1.m2
here, m1 and m2 methods are calling

Types of method 
There are two types of methods
1. Instance method
2. Static method

Instance method
- A method which is taking 'self' keyword as first parameter is called instance method
- Instance method address will be available within the object. Due to this reason, instance method we have to access by using the object.
Some questions you can create from above statement
- What is method?
- What keyword do you use as a first parameter?
- How do you access instance method address?

<Syntax to define instance method>
- To define a method or construct in python, we should prefix 'def' keyword.
def <methodname(Self)>
  <logic>
Example to define instance method
def MyFun(Self):
   print("MyFun is calling")
<SYNTAX to invoke instance method>
<objectname>.<instance methodname()>
eg, 
obj.MyFun()
here, obj is object name and
MyFun method name
What is an object?
- An object is an instance of a class
car is a class
  - your car is an object of class car
  - my car is an object of class car
pem is a class
   - your pen is an object
   - my pen is an object
Human is class
   - Ram is an object
   - Sam is an object of human class
instance is a physical representation
so, class is a logical representation
object is a physical representation of class

What an object contains?
-> An object contains instance variables and address addresses of the instance method.
<SYNTAX to create object>
<ObjectName>=<ClassName()>
for eg,
obj=MyClass()
eg,
class MyClass():
   def MyFun(self):
      print("MyFun is calling ..")
obj=MyClass()
obj.MyFun()


No comments:

Post a Comment

Git branch show detached HEAD

  Git branch show detached HEAD 1. List your branch $ git branch * (HEAD detached at f219e03)   00 2. Run re-set hard $ git reset --hard 3. ...